Time for action – setting up the test - Selenium

Imagine that you have a number of tests that work on a site that requires you to log in and move to a certain page. Or imagine that you need to have a test that requires you to be on a certain page. In these two situations the quickest way to find out which page you are on and then move to the correct one if need be, is to start testing. This is to make sure that we follow one of the major tenants of test automation, in that you always start from a known place. Let us see this in an example:

Create a new Java class in IDEA:

  1. Import the relevant Selenium Packages.

  2. Create the setup() and teardown() method. I prefer the JUnit 4 style of tests and will show code samples with the annotations.

  3. We need to check that the page is on the correct page. For this we will use the selenium.getTitle to see the page title and then if incorrect move to the chapter(Locators)link. We do this because navigating to page is slower than checking the page's title or any other calls to the page already loaded.

  4. We need to then validate that it is correct and then work accordingly. The following is a code snippet of how we can do this:

  5. Create the rest of the test to check that items are on the page.

What just happened?

We have just seen how we can check if something is what the test is expecting.If it is, the test will carry on as we expect. If it isn't what we expect, we can move our test to the correct page and then carry on with that page. We will see that if you log into the @Before, you may not start your tests.

Now let's have a look at how we can make more tests maintainable by splitting areas out into other methods.

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

Selenium Topics