title: how can i refactor acceptance tests with login
body: <p>Consider these two tests:</p>
  
  <pre><code>Imports OpenQA.Selenium
  Imports OpenQA.Selenium.Chrome
  
  &lt;TestClass()&gt; Public Class DSCSLoginTest
  
  &lt;TestMethod()&gt; Public Sub DSCSLoginLinkTest()
      Dim driver As New ChromeDriver()
      driver.Navigate.GoToUrl("http://myapp.com/landing.aspx")
  
      'click login
      driver.FindElementById("HeadLoginView_HeadLoginStatus").Click()
      Assert.AreEqual("http://myapp.com/Login.aspx", driver.Url)
  
      'fill in username
      driver.FindElementById("MainContent_Login1_txtUsername").SendKeys("user")
  
      'fill in password
      driver.FindElementById("MainContent_Login1_Password").SendKeys("pass")
  
      'click login
      driver.FindElementById("MainContent_Login1_LoginButton").Click()
  
      'test correct page
      Assert.AreEqual("http://myapp.com/loginok.aspx", driver.Url)
  
      'test correct title
      Dim title As String = driver.FindElementById("HeadContent_lblHeader").Text()
      Assert.AreEqual("Welcome to MyApp", title)
  End Sub
  
  &lt;TestMethod()&gt; Public Sub DSCSCantLoginWithoutPassword()
      Dim driver As New ChromeDriver()
      driver.Navigate.GoToUrl("http://myapp.com/landing.aspx")
  
      'click login
      driver.FindElementById("HeadLoginView_HeadLoginStatus").Click()
      Assert.AreEqual("http://myapp.com/Login.aspx", driver.Url)
  
      'fill in username
      driver.FindElementById("MainContent_Login1_txtUsername").SendKeys("user")
  
      'fill in password
      driver.FindElementById("MainContent_Login1_Password").SendKeys("")
  
      'click login
      driver.FindElementById("MainContent_Login1_LoginButton").Click()
  
      'test correct page
      Assert.AreEqual("http://myapp.com/Login.aspx", driver.Url)
  
      'test error recognition
      Dim validationResponse As String = driver.FindElementById("MainContent_Login1_PasswordRequired").Text
      Assert.AreEqual("*", validationResponse)
  End Sub
  End Class
  </code></pre>
  
  <p>You can see the browsing to the login part is duplicated. 
  There will be more tests that will depend on a user logging in first, so ideally I would like to refactor this into another class to be able to call something like 
      login()
  or
      loginAsAdmin()
  first, so I don't have to keep on repeating myself.</p>
  
  <p>Of course, these tests are not perfect. The fact I am relying on data in the db for users is already a smell, but these are tests being put into a legacy project and in some ways, an academic exercise.</p>
  
  <p>Ideally I'd like to have something like:</p>
  
  <pre><code>login() 
  </code></pre>
  
  <p>to encapsulate enough to get me logged in, for example:</p>
  
  <pre><code>Dim driver As New ChromeDriver()
      driver.Navigate.GoToUrl("http://myapp.com/landing.aspx")
  
      'click login
      driver.FindElementById("HeadLoginView_HeadLoginStatus").Click()
  
      'fill in username
      driver.FindElementById("MainContent_Login1_txtUsername").SendKeys("user")
  
      'fill in password
      driver.FindElementById("MainContent_Login1_Password").SendKeys("pass")
  
      'click login
      driver.FindElementById("MainContent_Login1_LoginButton").Click()
  
  End Sub
  </code></pre>
  
  <p>Could someone outline how I can go about this please?
  Thanks.</p>