Selenium grid with webdriver and testNG - java

I am experiencing the following problem. I have set up a grid with 2 nodes, in order to run tests in parallel.My suite.xml file has two groups, one for each node:
<suite name="testSuites" configfailurepolicy="continue" thread-count="2" parallel="tests">
test name="testSuite1" preserve-order="true">
<classes>
<class name="testA1" />
<class name="testB1" />
<class name="testC1" />
</classes>
</test>
<test name="testSuite2" preserve-order="true">
<classes>
<class name="testA2" />
<class name="testB2" />
<class name="testC2" />
</classes>
</test>
Each class, for example testA1 has the following testNG configuration:
#BeforeClass(alwaysRun = true)
public void createInitialData() {
}
#Test(alwaysRun = true, description = "bla bla")
public void testStep_1() throws Exception{
}
#Test(alwaysRun = true, description = "bla bla ", dependsOnMethods ="testStep_1" )
public void testStep_2() {
}
Upon running I noticed that during the execution, the tests are executing in test step wise order, meaning:
testA1-testStep_1, testB1-testStep_1, testC1-testStep_1, testA1-testStep_2, testB1-testStep_2, testC1-testStep_2
whereas it should have been:
testA1-testStep_1, testA1-testStep_2, and then testB1-testStep_1, testB1-testStep_2, testC1-testStep_1, testC1-testStep_2
Any suggestions?

Try to set group-by-instances in your xml
<suite group-by-instances="true">
or
<test group-by-instances="true">

<test name="testSuite2" parallel="false">
Seem also be doing what you need.

Related

Appium / selenium: testNG parallel execution not working

i am using appium and selenium.
i am trying to rum it parallel (one case after another case)
my first case (forgot password is running properly ) and after that execution is just stop.
can anyone help me with this?
i have attached testng.xml , and testbase file. also login and forgot password scripts.
i guess there is some issues with annotations.
i have tried few but now working.
can anyone help me with this?
Thanks!!!
Here my code looks like:
1.testng.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite">
<test thread-count="5" name="Test" parallel="classes">
<classes>
<class name="com.live.testcase.TC0001ForgotPassword" />
<class name="com.live.testcase.TC0002Login" />
<class name="com.live.testcase.TC0003Dashboard" />
<class name="com.live.testcase.TC0004Activity" />
<class name="com.live.testcase.TC0005MoveMoney" />
<class name="com.live.testcase.TC0006InternationalTransfer" />
<class name="com.live.testcase.TC0007Integration" />
<class name="com.live.testcase.TC0008Account" />
</classes>
</test> <!-- Test -->
</suite> <!-- Suite -->
2. testbase.java
public class testBase {
private String reportDirectory = "reports";
private String reportFormat = "xml";
private String testName = "Untitled";
protected IOSDriver<IOSElement> driver = null;
#BeforeSuite
public void setup() throws MalformedURLException {
DesiredCapabilities dc = new DesiredCapabilities();
dc.setCapability("reportDirectory", reportDirectory);
dc.setCapability("reportFormat", reportFormat);
dc.setCapability("testName", testName);
dc.setCapability(MobileCapabilityType.UDID, "afb65172e9b47b01482d912dede58515819748a3");
dc.setCapability(IOSMobileCapabilityType.BUNDLE_ID, "com.novo.ios.dev");
driver = new IOSDriver<IOSElement>(new URL("http://localhost:4723/wd/hub"), dc);
driver.setLogLevel(Level.INFO);
}
#AfterSuite
public void teardown() {
}
}
3.Forgot password
public class TC0001ForgotPassword extends testBase {
#Test
public void ForgotPassword() throws InterruptedException {
// Test case for blank email address.
driver.findElement(By.xpath("//*[#text='Forgot Password?']")).click();
driver.findElement(By.xpath("//*[#placeholder='Email']")).sendKeys("Automationtesting#banknovo.com");
driver.findElement(By.xpath("//*[#text='Done']")).click();
Thread.sleep(1000);
driver.findElement(By.xpath("//*[#text='CONFIRM']")).click();
Thread.sleep(5000);
driver.findElement(By.xpath("//*[#text='(MM/DD/YYYY)']")).click();
Thread.sleep(2000);
driver.findElement(By.xpath("//*[#text='Done']")).click();
Thread.sleep(2000);
driver.findElement(By.xpath("//*[#text='CONFIRM']")).click();
Thread.sleep(2000);
driver.findElement(By.xpath("//*[#text='1']")).click();
driver.findElement(By.xpath("//*[#text='2']")).click();
driver.findElement(By.xpath("//*[#text='3']")).click();
driver.findElement(By.xpath("//*[#text='4']")).click();
Thread.sleep(2000);
driver.findElement(By.xpath("//*[#text='CONFIRM']")).click();
Thread.sleep(2000);
driver.findElement(By.xpath("//*[#text='CONFIRM']")).sendKeys("111111");
Thread.sleep(2000);
driver.findElement(By.xpath("//*[#text='CONFIRM']")).click();
Thread.sleep(2000);
driver.findElement(By.xpath("//*[#placeholder='Password']")).sendKeys("Novo#2019");
driver.findElement(By.xpath("//*[#placeholder='Confirm Password']")).sendKeys("Novo#2019");
Thread.sleep(2000);
driver.findElement(
By.xpath("(//*[#class='UIAView' and ./parent::*[#class='UIAScrollView']]/*[#text='icEyeOpen'])[1]"))
.click();
driver.findElement(By.xpath("//*[#text='icEyeOpen']")).click();
Thread.sleep(2000);
driver.findElement(By.xpath("//*[#text='RESET PASSWORD']")).click();
Thread.sleep(2000);
driver.findElement(By.xpath("//*[#text='DONE']")).click();
}
4. Login
package com.live.testcase;
import org.testng.annotations.Test;
import org.openqa.selenium.By;
import com.live.common.testBase;
public class TC0002Login extends testBase {
#Test
public void TC000001_Blank_Email_Password() {
// Test case for blank email address.
driver.findElement(By.xpath("//*[#text='LOG IN']")).click();
driver.findElement(By.xpath("//*[#text='OK']")).click();
}
#Test
public void TC000002_Invailid_Email() throws Exception {
// Test case for invalid email addresses
driver.findElement(By.xpath("//*[#placeholder='Email']")).sendKeys("automationtesting");
driver.findElement(By.xpath("//*[#placeholder='Password']")).sendKeys("Novo");
driver.findElement(By.xpath("//*[#text='LOG IN']")).click();
driver.findElement(By.xpath("//*[#text='OK']")).click();
}
#Test
public void TC000003_Invailid_Password() throws Exception {
// Test case for invalid email addresses
driver.findElement(By.xpath("//*[#placeholder='Email']")).clear();
driver.findElement(By.xpath("//*[#placeholder='Password']")).clear();
driver.findElement(By.xpath("//*[#placeholder='Password']")).sendKeys("Novo#2019");
driver.findElement(By.xpath("//*[#text='LOG IN']")).click();
driver.findElement(By.xpath("//*[#text='OK']")).click();
}
#Test
public void TC000004_Valid_Email_Password() throws Exception {
// Test case for Valid email addresses & password
driver.findElement(By.xpath("//*[#placeholder='Email']")).sendKeys("automationtesting#banknovo.com");
driver.findElement(By.xpath("//*[#placeholder='Password']")).sendKeys("Novo#2019");
driver.findElement(By.xpath("//*[#text='LOG IN']")).click();
}
}
5. pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.novo.app</groupId>
<artifactId>com.novo.app</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>io.appium</groupId>
<artifactId>java-client</artifactId>
<version>7.1.0</version>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.14.3</version>
</dependency>
</dependencies>
</project>
I assume this could be the issue:
You instruct testng to run parallel at suite tag not at test tag.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite" thread-count="5" parallel="classes">
<test name="Test">
<classes>
<class name="com.live.testcase.TC0001ForgotPassword" />
<class name="com.live.testcase.TC0002Login" />
<class name="com.live.testcase.TC0003Dashboard" />
<class name="com.live.testcase.TC0004Activity" />
<class name="com.live.testcase.TC0005MoveMoney" />
<class name="com.live.testcase.TC0006InternationalTransfer" />
<class name="com.live.testcase.TC0007Integration" />
<class name="com.live.testcase.TC0008Account" />
</classes>
</test> <!-- Test -->
As per Parallelism and time-outs chapter:
The parallel attribute on the <suite> tag can take one of following values:
<suite name="My suite" parallel="methods" thread-count="5">
<suite name="My suite" parallel="tests" thread-count="5">
<suite name="My suite" parallel="classes" thread-count="5">
<suite name="My suite" parallel="instances" thread-count="5">
So my expectation is that you need to amend your testng.xml to look like:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite" parallel="tests" configfailurepolicy="continue" verbose="2">
<test name="TC0001ForgotPassword">
<classes>
<class name="com.live.testcase.TC0001ForgotPassword"/>
</classes>
</test>
<test name="TC0002Login">
<classes>
<class name="com.live.testcase.TC0002Login"/>
</classes>
</test>
<test name="TC0003Dashboard">
<classes>
<class name="com.live.testcase.TC0003Dashboard"/>
</classes>
</test>
<test name="TC0004Activity">
<classes>
<class name="com.live.testcase.TC0004Activity"/>
</classes>
</test>
<test name="TC0005MoveMoney">
<classes>
<class name="com.live.testcase.TC0005MoveMoney"/>
</classes>
</test>
<test name="TC0006InternationalTransfer">
<classes>
<class name="com.live.testcase.TC0006InternationalTransfer"/>
</classes>
</test>
<test name="TC0007Integration">
<classes>
<class name="com.live.testcase.TC0007Integration"/>
</classes>
</test>
<test name="TC0008Account">
<classes>
<class name="com.live.testcase.TC0008Account"/>
</classes>
</test>
</suite>
More information including sample project: Parallel Tests Execution

TestNG Based Selenium Tests not running in Parallel

I am using the below TestNG Config to enable parallel execution of Selenium tests.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Test-Automation" parallel="methods" thread-count="2" verbose="1">
<test name="Suite Test">
<classes>
<class name="SampleTest">
<methods>
<include name="firstTest"/>
<include name="secondTest"/>
<include name="thirdTest"/>
</methods>
</class>
</classes>
</test>
</suite>
Java Code:
#Test(dataProvider = "TestData")
public void firstTest(String data){
//Code
}
#Test(dataProvider = "TestData")
public void secondTest(String data){
//Code
}
#Test(dataProvider = "TestData")
public void thirdTest(String data){
//Code
}
The Selenium tests are expected to run in parallel. I expect 2 browsers to be open and run the test script.
But I see only 1 browser and all 3 tests run one after the other and not in parallel. I have tried using test, methods, class, instance options for "parallel" attribute.
Any help?
This is due to a bug in TestNG 6.13.1 [ See GITHUB-1636 for more details ]
I have fixed this in the latest SNAPSHOT of TestNG (6.14-SNAPSHOT) and this should be available for use in the released version of TestNG (6.14).
But until then, please alter your suite xml file to look like below :
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Test-Automation" parallel="methods" thread-count="2" verbose="1">
<test name="Suite Test" parallel="methods" thread-count="2" verbose="1">
<classes>
<class name="SampleTest">
<methods>
<include name="firstTest"/>
<include name="secondTest"/>
<include name="thirdTest"/>
</methods>
</class>
</classes>
</test>
</suite>
The work-around is basically to add the attributes parallel="methods" thread-count="2" at the <test> level also.
Seperate all the test and then try with parallel="test"
<test name="Suite Test1">
<classes>
<class name="//..//packg name..SampleTest">
</class>
</classes>
</test>
<test name="Suite Test2">
<classes>
<class name="//..//SampleTest">
</class>
</classes>
</test>
<test name="Suite Test3">
<classes>
<class name="//..//packg name..SampleTest">
</class>
</classes>
</test>
</suite>

Unable to run the Regression group in testng.xml file for different browsers

I have configured the testng.xml file to run the Regression group in different browsers.Below is the testng.xml code for the same.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="SeleniumSuite" verbose ="1" thread-count = "1" parallel="false">
<**test** name="FirefoxTest">
<groups>
<run>
<include name="Regression"></include>
</run>
</groups>
<parameter name="browser" value="firefox" />
<classes>
<class name="Testscript.Program111_RediffLogin" />
</classes>
</test>
<test name="IETest">
<groups>
<run>
<include name="Regression"></include>
</run>
</groups>
<parameter name="browser" value="ie" />
<classes>
<class name="Testscript.Program111_RediffLogin" />
</classes>
</test>
</suite>
when I hover the mouse on the tag, it displays an error message as "The content of element type "test" must match "(method-selectors?,parameter*,groups?,packages?,classes?)".At the Test class level I have defined all the parameters properly for the regression test to run.But still I am seeing the error in the testng.xml file.Can any one of you look into this and help me!
Please find the test case I am using for the automation
#Test(groups={"Regression"},dataProvider = "hashmapdataprovider",dataProviderClass =Dataprovider.Dataprovider_Hashmap.class,priority=1 )
public void validLogin(Map<String,String> hm) throws IOException
{
pageobjects.Signin();
driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
OriginalExcelRW Excel = new OriginalExcelRW("F:\\anand_acer\\selenium\\rediffbooks.xlsx");
XSSFSheet s1 = Excel.Setsheet("Sheet1");
SoftAssert s_assert = new SoftAssert();
if (hm.get("Executionflow").contains("anand"))
{
pageobjects.Username1(hm.get(Excel.Readvalue(s1, 0, 2)));
pageobjects.pass1(hm.get(Excel.Readvalue(s1, 0, 3)));
//s_assert.assertEquals(hm.get(Excel.Readvalue(s1, 0, 2)), hm.get(Excel.Readvalue(s1, 0, 3)), "both the usssser Ideee and password doesnt matches");
//logger.info("Usssser Ideeee");
pageobjects.login();
s_assert.assertTrue(true, "login success");
//logger.info("The login was success");
System.out.println("Valid login is passed");
pageobjects.signout();
pageobjects.Signin();
//pageobjects.cleartext();
}
s_assert.assertAll();
}
Looks like from the error message you have posted they must be in a special order of indentation?
Here is an example of my XML which works
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite verbose="1" name="example suite 1">
<listeners>
<listener class-name="ReportListener.ReportListenerClass" />
</listeners>
<test name="Logout tests">
<classes>
<class name="com.emc.qe.u360.tests.LogoutTests" />
</classes>
</test>
<test name="Login tests">
<classes>
<class name="com.emc.qe.u360.tests.LoginPageTests" />
</classes>
</test>
</suite>

Selenium: Run same test on different browsers

I want to run same test on different browsers (Firefox, Chrome and Safari). To do this I am using parametrization in tests. Where in if browser is chrome run chrome and so on.
And in testng.xml I have specified to run tests in parallel and repeated same test in under several <test> tags.
What if I have so many tests to run. I will be copy pasting same test in <test> tags. Which doesn't see optimal solution.
How can I fix this?
Here is what I tried:
public class SameTestDifferentBrowsers {
WebDriver driver;
#BeforeMethod
#Parameters("browser")
public void openBroswer(String browser) {
if (browser.equalsIgnoreCase("Chrome")) {
System.out.println(System.getProperty("user.dir"));
System.setProperty("webdriver.chrome.driver", System.getProperty("user.dir")+"//lib//browsers//chromedriverMac");
driver = new ChromeDriver();
} else if (browser.equalsIgnoreCase("Firefox")) {
driver = new FirefoxDriver();
} else if (browser.equalsIgnoreCase("safari")) {
System.setProperty("webdriver.safari.driver", System.getProperty("user.dir")+"//lib//browsers//SafariDriver.safariextz");
driver = new SafariDriver();
}
}
#AfterMethod
public void closeBrowser() {
driver.quit();
}
#Test
public void login_TestCase() {
driver.get("https://www.google.com");
}
}
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Parallel test suite" parallel="tests">
<test name="Firefox Test">
<parameter name="browser" value="browser"/>
<classes>
<class name="ParallelBrowser.SameTestDifferentBrowsers"/>
</classes>
</test>
<test name="Chrome Test">
<parameter name="browser" value="browser"/>
<classes>
<class name="ParallelBrowser.SameTestDifferentBrowsers"/>
</classes>
</test>
<test name="Safari Test">
<parameter name="browser" value="browser"/>
<classes>
<class name="ParallelBrowser.SameTestDifferentBrowsers"/>
</classes>
</test>
</suite>
If you are sure that you would want to run all the tests for all the three browsers then you may change your design a little.
Create multiple test classes for different test cases. In your xml test tags pass different test class names as parameters.
Write three #test methods in your parametrized test class each for each browser that accepts the class name as a parameter. Now using Reflections you can invoke the right test method in every #test method.
Two things require fixing here and the problem sill be solved.
First: Update the TestNG.xml file to include the browser names
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Parallel test suite" parallel="tests">
<test name="Firefox Test">
<parameter name="browser" value="Firefox" />
<classes>
<class name="ParallelBrowser.SameTestDifferentBrowsers" />
</classes>
</test>
<test name="Chrome Test">
<parameter name="browser" value="Chrome" />
<classes>
<class name="ParallelBrowser.SameTestDifferentBrowsers" />
</classes>
</test>
<test name="Safari Test">
<parameter name="browser" value="safari" />
<classes>
<class name="ParallelBrowser.SameTestDifferentBrowsers" />
</classes>
</test>
Second: Safari browser extension should be installed and enabled in the browser and shouldn't be kept in the path.
So just comment following line from Java Code File
//System.setProperty("webdriver.safari.driver",
//System.getProperty("user.dir") + "/lib/browsers/SafariDriver.safariextz");

how to run more then one selenium testing suite on the same machine

I am using Java/Selenium webdriver with testng to run my test automation, i have many automated project, each project is using a testing suite.xml, how can i run two suites or more at the same time on the same machine,
here is my code for the creation of the driverInstance object:
public WebDriver getDriverInstance(
String Url,
String browser ) throws MalformedURLException {
WebDriver driver = null;
URL url = new URL( Url );
if( browser.equals( "firefox" ) ) {
DesiredCapabilities capability = DesiredCapabilities.firefox();
driver = new RemoteWebDriver( url, capability );
} else if( browser.equals( "chrome" ) ) {
DesiredCapabilities capability = DesiredCapabilities.chrome();
driver = new RemoteWebDriver( url, capability );
} else if( browser.equals( "IE" ) ) {
DesiredCapabilities capability = DesiredCapabilities.internetExplorer();
driver = new RemoteWebDriver( url, capability );
}
return driver;
}
You need set attribute as parallel="tests" in your testng xml
<suite name="Parallel test runs" parallel="tests" thread-count="2">
There is the “parallel” parameter. Set to “tests” because we want to run tests parallel. The other parameter is the “thread-count”. If it is set to 2, than two browsers will be opened and the first two tests will run from the list. If the thread-count is 5 than five browsers will be opened and all the five tests will executed parallel!
For tests your testng.xml structure should be like below:-
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Parallel test runs" parallel="tests" thread-count="2">
<test name="T_01">
<classes>
<class name="testNG.Parallel.Test01" ></class>
</classes>
</test>
<test name="T_02">
<classes>
<class name="testNG.Parallel.Test02" ></class>
</classes>
</test>
<test name="T_03">
<classes>
<class name="testNG.Parallel.Test03" ></class>
</classes>
</test>
<test name="T_04">
<classes>
<class name="testNG.Parallel.Test04" ></class>
</classes>
</test>
<test name="T_05">
<classes>
<class name="testNG.Parallel.Test05" ></class>
</classes>
</test>
</suite>
If you want to run 2 classes parallelly
<suite name="Parallel test suite" parallel="classes" thread-count="2">
For classes your testng.xml structure should be like below:-
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Parallel test suite" parallel="classes" thread-count="2">
<test name="Test 1">
<classes>
<class name="com.parallel.TestParallelClassOne"/>
<class name="com.parallel.TestParallelClassTwo"/>
</classes>
</test>
</suite>
Now the thing is you need to add both project classes in different packages in a same project and use my 1 example testng structure and add the attribute as above.
How to run two suites:-
<suite name="allSuites">
<suite-files>
<suite-file path="suite1.xml" />
<suite-file path="suite2.xml" />
...
</suite-files>
</suite>
Hope it will help you :)
Get back to me if still facing any issue :)

Categories