简体   繁体   中英

how can i refactor acceptance tests with login

Consider these two tests:

Imports OpenQA.Selenium
Imports OpenQA.Selenium.Chrome

<TestClass()> Public Class DSCSLoginTest

<TestMethod()> 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

<TestMethod()> 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

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.

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.

Ideally I'd like to have something like:

login() 

to encapsulate enough to get me logged in, for example:

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

Could someone outline how I can go about this please? Thanks.

Maybe like:

Public Sub Login(dr as ChromeDriver, user as String, pass as String)
    dr.Navigate.GoToUrl("http://myapp.com/landing.aspx")
    dr.FindElementById("HeadLoginView_HeadLoginStatus").Click()
    dr.FindElementById("MainContent_Login1_txtUsername").SendKeys(user)
    dr.FindElementById("MainContent_Login1_Password").SendKeys(pass)
    dr.FindElementById("MainContent_Login1_LoginButton").Click()
End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM