Saturday, November 5, 2016

Using dependencies and priority in TestNG tests: Ordering tests execution

Suppose there are three tests in a test class which needs to be executed, we can define the tests in the class as shown below:


public class TestOrder {
@Test
public void TestA() {
System.out.println("test a");
}

@Test
public void TestB() {
System.out.println("test b");
}

@Test
public void TestC() {
System.out.println("test c");
}
}


When we execute the test class, all the three tests are executed, but order of the test is not sure. We can prioritize the test using priority.


public class TestOrder {
@Test(priority=1)
public void TestA() {
System.out.println("test a");
}

@Test(priority=2)
public void TestB() {
System.out.println("test b");
}

@Test(priority=3)
public void TestC() {
System.out.println("test c");
}
}


Since there can be many tests in a test suite, and it can be difficult to prioritize tests based on numbering tests. In such cases,we can prioritize the tests based on dependsongroup or dependsonmethod as shown below.  Using groups, we can group the tests based on functionality and scope of the tests




public class TestOrder {
@Test(groups = { "smoke" })
public void TestA() {
System.out.println("test a");
}

@Test(dependsOnMethods = { "TestD" }, groups = { "regression" })
public void TestB() {
System.out.println("test b");
}

@Test(dependsOnGroups = { "smoke" })
public void TestC() {
System.out.println("test c");
}

@Test
public void TestD() {
System.out.println("test d");
}
}

How to run multiple test suites in TestNG?

Suppose we have individual test suite for different features/modules in TestNG, we can run multiple testNG test from a master suite xml file by calling the individual tests as shown below:


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="MainTestSuite">
    <suite-files>
        <suite-file path="suiteFeatureA.xml" />  
        <suite-file path="suiteFeatureB.xml" /> 
        <suite-file path="suiteFeatureC.xml" />  
        <suite-file path="suiteFeatureD.xml" />      
    </suite-files>
</suite>


This way, we can run multiple test suites from a master suite file in TestNG.

Thursday, November 3, 2016

Handling authentication popup in Selenium WebDriver

In case of login into an application in selenium, an alert pop may be displayed authenticating for user credential. The pop-up can be web based or windows based popup.

Web Based Authentication pop-up can be handled using alert class as shown below:

// provide a wait condition for alert to be present
WebDriverWait wait = new WebDriverWait(driver, 30);
Alert alert = wait.until(ExpectedConditions.alertIsPresent());
//Using alert.authenticateUsing, provide username and password for http alert popup     
alert.authenticateUsing(new UserAndPassword( ** username ** , ** password ** ));

Or else, can try the below code and see it works:

driver.Navigate().GoToUrl("http://UserName:Password@testingmail.com");

This was about web-based pop-up, In case a windows pop-up appears asking for username and password, An autoIT script can be created to handle the windows pop-up.
  • Create an au3 file for handling the pop-up.
  • Compile the au3 file using AutoIT script  to exe converter.
autoit script converter

  • Run the autoIT exe file using below code in selenium.

Runtime.getRuntime().exec("d:\\AuthenticateAutoIt.exe");