"No one is harder on a talented person than the person themselves" - Linda Wilkinson ; "Trust your guts and don't follow the herd" ; "Validate direction not destination" ;

October 27, 2011

Automation Assignment - Selenium, TestNG - Improvements Post

[Previous post in this series - Automation Assignment - Selenium, TestNG]

Based on previous post. I got couple of comments on improvements for this code. The enhancements done are
  • Soft Assertions
  • Removing Thread Sleep code. WaitForCondition is used instead of it
  • Configuring Page Load using a common timeout value
From Selenium documentation found difference between assert and verify
  • If you use an assert, the test will stop at that point and not run any subsequent checks.
  • In contrast, verify commands will not terminate the test
Handling Soft Assertions is done based on Selenium Example post. Modified HomePageTest.java as below. You can download files for Soft-Assertions and add it to the project. As you can see below HomePage extends TestBase class present in Soft Assertions.

import java.io.File;
import java.io.FileOutputStream;
import org.testng.annotations.*;
import com.thoughtworks.selenium.DefaultSelenium;
import com.thoughtworks.selenium.Selenium;

public class HomePageTest extends TestBase {
      Selenium selenium;
                        public static final String WAITFORPAGELOADTIME = "30000";
                       
      @Parameters( { "browserpath", "appurl"})
      @BeforeTest(alwaysRun = true)
      public void init(String browserpath, String appurl) {
            selenium = new DefaultSelenium("localhost", 4444, browserpath, appurl);
            selenium.start();
            selenium.deleteAllVisibleCookies();
            selenium.refresh();
      }

      @AfterTest
      public void tearDown() {
            selenium.stop();
      }
     
      /* Test for 1 way flight  */
      @Test(dataProvider = "dataProviderSearchFlightsOneWay")
      public void SearchFlightsOneWay(String from, String to) {
            try {
                  selenium.open("/");
                  selenium.type("//input[@id='origin_autocomplete']", from);
                  selenium.type("//input[@id='destination_autocomplete']", to);
                  selenium.click("//img[@alt='Calendar']");
                  selenium.click("link=20");
                  selenium.click("button_flight_search");
                  selenium.waitForPageToLoad(WAITFORPAGELOADTIME);
                  verifyResults("1");
            } catch (Exception e) {
                  System.out.println("Exception Occurred " + e.toString());
            }

      }

      /* Test for round trip flight  */
      @Test
      public void SearchFlightsRoundTrip() {
            try {
                  selenium.open("/");
                  selenium.type("//input[@id='origin_autocomplete']", "MAA");
                  selenium.type("//input[@id='destination_autocomplete']", "BLR");
                  selenium.click("//img[@alt='Calendar']");
                  selenium.click("link=17");
                  selenium.click("id=rnd_trip");
                  selenium.click("//*[@id='rtn_date']");
                  selenium.click("link=30");
                  selenium.click("button_flight_search");
                  selenium.waitForPageToLoad(WAITFORPAGELOADTIME);
                  verifyResults("2");
            } catch (Exception e) {
                  System.out.println("Exception Occurred " + e.toString());
            }
      }

      /* verifier for search results  */
      public void verifyResults(String Filter) {
            selenium.waitForCondition("selenium.isElementPresent(\"//*[@id='mod_link']\");",
            WAITFORPAGELOADTIME);
            verifyTrue(selenium.isElementPresent("mod_link"));
            verifyTrue(selenium.isElementPresent("facebookLink"));
      }

      /* Test for train search  */
      @Test
      public void testTrainSearch() {
            try {
                  selenium.open("/");
                  selenium.click("link=Trains");
                  selenium.waitForPageToLoad(WAITFORPAGELOADTIME);
                  selenium.type("from_station", "chennai");
                  selenium.keyPressNative(java.awt.event.KeyEvent.VK_TAB + "");
                  selenium.type("to_station", "bangalore");
                  selenium.keyPressNative(java.awt.event.KeyEvent.VK_TAB + "");
                  selenium.select("trainClass", "label=AC 2 Tier (2A)");
                  selenium.click("css=img[alt=\"Calendar\"]");
                  selenium.click("//a[contains(text(),'17')]");
                  selenium.keyPressNative(java.awt.event.KeyEvent.VK_TAB + "");
                  selenium.click("//button[@type='submit']");
                  selenium.waitForCondition(
                              "selenium.isElementPresent('id=mod_link')", "60000");
            } catch (Exception e) {
                  System.out.println("Exception Occurred " + e.toString());
            }

      }

      /* Test for Hotels Search  */
      @Test
      public void searchHotels() {
            try {

                  selenium.open("/");
                  selenium.click("link=Hotels");
                  selenium.waitForPageToLoad(WAITFORPAGELOADTIME);
                  selenium.type("city", "chennai");
                  selenium.click("//img[@alt='Calendar']");
                  selenium.click("link=12");
                  selenium.keyPressNative(java.awt.event.KeyEvent.VK_TAB + "");
                  selenium.click("//img[@alt='Calendar']");
                  selenium.click("link=29");
                  selenium.click("//button[@type='submit']");
                  selenium.waitForPageToLoad(WAITFORPAGELOADTIME);
                  selenium.waitForCondition(
                              "selenium.isElementPresent('id=mod_link')", "60000");
            } catch (Exception e) {
                  System.out.println("Exception Occurred " + e.toString());
            }
      }

      /* Test to verify headers  */
      @Test
      public void verifyHeaders() {
            try {
                  selenium.open("/");
                  verifyTrue(selenium.isElementPresent("link=Hotels"));
                  verifyTrue(selenium.isTextPresent("Hotels"));
                  verifyEquals("Trains", selenium.getText("link=Trains"));
                  verifyTrue(selenium.isElementPresent("link=Trains"));
                  verifyEquals("Small World", selenium
                              .getText("link=Small World"));
                  verifyTrue(selenium.isElementPresent("link=Small World")); 
                  verifyEquals("more", selenium.getText("link=more"));
                  verifyTrue(selenium.isElementPresent("link=more"));
                  verifyTrue(selenium.isElementPresent("link=My Trips"));
                  verifyTrue(selenium.isElementPresent("link=Sign in"));
                  verifyTrue(selenium.isElementPresent("link=Register"));
                  verifyTrue(selenium.isElementPresent("link=Register"));
                  verifyTrue(selenium
                              .isElementPresent("//ul[@id='global']/li[5]/a/strong"));
                  verifyEquals("Tell us what you think", selenium
                              .getText("//ul[@id='global']/li[5]/a/strong"));
                  verifyTrue(selenium
                              .isElementPresent("//ul[@id='global']/li[5]/a/strong"));
            } catch (Exception e) {
                  System.out.println("Exception Occurred " + e.toString());
            }

      }

      /* Test to verify footers  */
      @Test
      public void verifyFooter() {
            try {
                  selenium.open("/");
                  verifyTrue(selenium.isElementPresent("link=About Us"));
                  verifyTrue(selenium.isElementPresent("link=FAQs"));
                  verifyTrue(selenium.isElementPresent("link=FAQs"));
                  verifyTrue(selenium.isElementPresent("link=Support"));
                  verifyTrue(selenium.isElementPresent("link=Customer Forum"));
                  verifyTrue(selenium.isElementPresent("link=Blog"));
                  verifyTrue(selenium
                              .isElementPresent("link=Cleartrip for Business"));
                  verifyTrue(selenium.isElementPresent("link=Privacy"));
                  verifyTrue(selenium.isElementPresent("link=Security"));
                  verifyEquals("Security", selenium.getText("link=Security"));
                  verifyTrue(selenium.isTextPresent("API"));
                  verifyTrue(selenium.isTextPresent("Terms of Use"));
                  verifyTrue(selenium.isTextPresent("Privacy"));
                  verifyTrue(selenium.isTextPresent("Cleartrip for Business"));
                  verifyTrue(selenium.isTextPresent("Blog"));
                  verifyTrue(selenium.isTextPresent("Customer Forum"));
                  verifyTrue(selenium.isTextPresent("Support"));
                  verifyTrue(selenium.isTextPresent("About Us"));
            } catch (Exception e) {
                  System.out.println("Exception Occurred " + e.toString());
            }

      }

      /* Data Provider for oneway flight Search */
      @DataProvider(name = "dataProviderSearchFlightsOneWay")
      public Object[][] setDataforSearchFlightsOneWay() {
            System.out.println("Start Data Provider: SearchFlightsOneWay");
            Object[][] retkeyword = { { "MAA", "BLR" }, { "MAA", "BOM" } };
            System.out.println("End Data Provider: SearchFlightsOneWay");
            return (retkeyword);
      }
}

Please find snapshot of files in this project. Please download them from previous post.
This may not be perfect code but this is one step ahead of my previous code. Many thanks to Tarun for his feedback for previous post.

More Reads
Taking Remote Screenshots With Selenium And The Robot Framework
Taking Snapshots
Parameterization of Selenium Tests with Microsoft Excel

Happy Reading!!!

2 comments:

tarun k said...

Hi Shiva,

My first suggestion to you is to use Selenium 2 instead of Selenium 1 you would find umpteen reasons on Google as to why Selenium 2 is better than Selenium 1. Coming back to test design -

1. Try to follow page object pattern, it would make your tests way cleaner - http://code.google.com/p/selenium/wiki/PageObjects

2. You have lots of verifyTrue in your tests, like - verifyHeaders(). Could you loop them through so that you don't have to repeat statements time and again. Look here for more on this. I had blogged on this - http://seleniumsoftwaretesting.blogspot.com/2011/05/verification-of-application-element.html

3. When you use soft assertion then You need to check for verification error at the end of your tests else your tests would never fail. Inject a failure in your tests and see if it fails or not.

4. Don't use "default package" in eclipse and create your own package.

At last, thanks for sharing your work, I appreciate your perseverance towards learning the Selenium :)

Siva said...

Thanks Tarun. This feedback helps alot. Looking forward for next post with this changes incorporated.