1

SCENARIO OF TEST CASE 1:

  • Opens the browser
  • Enters the username password
  • Goes to home page
  • Closes the browser

SCENARIO OF TEST CASE 2:

Home page should be testes so the baseurl for this case is home page:

  • Open BaseUrl i.e. home page
  • Then clicks the tabs of home pages.
    In order to run TEST CASE 2 first the browser should login which is TEST CASE 1.

Question - How can I handle such interdependent cases using Selenium WebDriver

  • How about not destroying the drive object and closing your browser till all your cases are executed? You opened a browser in the first case, continue testing with the second case and then close the browser! – IAmMilinPatel Sep 30 '16 at 5:07
  • @TESTasy I've 19 test cases how can I define the driver instance to quit when the 19th test case is completed? – TechJhola Sep 30 '16 at 5:09
  • Do you really want to close the browser in first test case ?? – QAMember Sep 30 '16 at 5:10
  • @QAMember in my framework the browserbase consists of two functions first [SetUp] which initialzes the browser and second [TearDown] which exits the browser. this runs all at once. so every time any of the case runs the browser exits immediately after the first test case is run – TechJhola Sep 30 '16 at 5:18
  • 1
    @TechJola, So write a different test methods for each scenario and keep the logic/code up to login in setUp method – QAMember Sep 30 '16 at 5:21
3

You can try someting like,

Option 1

  1. Open browser.

  2. Open Base URL.

Scenario 1

  1. Go to Login page.

  2. Enter the username and password.

  3. Login. Lands on the Home Page/Dashboard/Whatever.

Scenario 2

  1. Goes to home page (User still logged in).

  2. Click Tab1. Perform actions to be tested.

  3. Click Tab2. Perform actions to be tested.

  4. ...

  5. Click TabN. Perform actions to be tested.

  6. End Of Tests

  7. Destroy driver object.

  8. Close browser.


Option 2

Scenario 1

  1. Open Browser

  2. Go to Login page.

  3. Enter the username and password.

  4. Login. Lands on the Home Page/Dashboard/Whatever.

  5. Destroy driver object.

  6. Close browser.

Scenario 2

  1. Open Browser

  2. Go to Login page.

  3. Enter the username and password.

  4. Login. Lands on the Home Page/Dashboard/Whatever.

  5. Goes to home page (User still logged in).

  6. Click Tab1. Perform actions to be tested.

  7. Click Tab2. Perform actions to be tested.

  8. ...

  9. Click TabN. Perform actions to be tested.

  10. Destroy driver object.

  11. Close browser.


Personally I'd go with Option 1!!!

  • 1
    Test Reporting might be a problem with 'Option 1'. The 'Option 2' is actually neat if the OP has organized reusable methods for different units. – Ramnath Sep 30 '16 at 5:36
  • Option 1 is actually a poor design. If there is a failure somewhere in the test or a different framework is set up that runs the tests in parallel or the order of the tests change or 1 in a slew of other issues occur, all of your tests fail without a proper reasoning as to why. – Paul Muir Oct 21 '16 at 19:42
2

This is a very common problem.

First, if you haven't already done so, you need to refactor your test code to put the login routine into a helper method. Your helpers should be used to minimize repeated code.

I'd approach the test code this way:

// initialization
[TestInitialize]
// method that calls the browser open routine and goes to the login page 

[Test]
public void Test1 () 
    {
        // call the login method for user X
        // assert that you're actually logged in as the expected user
    }

[Test]
public void Test2()
    {
        // call the login method
        // call the first tab method
        // assert that you're on the first tab
    }

// Add test methods for each tab using the same pattern as Test2

[TearDown]
// call the browser close method after each test

This keeps your test code clean, lets you use actions you test one test as setup steps for a second test, and ensures that every test has proper separation of concerns.

If you find you're stringing together a lot of method calls in your tests, build higher level calls to do this, so instead of calling login, then go to tab 1 for a test of something on tab 1, you'd call something like LogonAndGoToTab1() which in turn calls the login function then the go to tab 1 function.

1

Use [TestInitialize] and add Open browser ,Enter user name/password ,Go to home page Steps inside the [TestInitialize].

Then every time you run a test i will always do the above 3 steps before executing a test.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.