TestNG Parameter behaving inconsistently when executed multiple time - selenium

I want to run multi-browser testing. For that, this is my testng.xml
<suite name="MultiBrowsreTest" parallel="tests">
<test name="T1" >
<parameter name="browser" value="firefox"/>
<classes>
<class name="com.core.My"/>
</classes>
</test>
<test name="T2" >
<parameter name="browser" value="chrome"/>
<classes>
<class name="com.core.My"/>
</classes>
</test>
</suite>
And Here is my Java program.
public class My {
HH h ;
#Test
#Parameters("browser")
public void my1(String browser){
h = new HH();
h.browser = browser;
System.out.println("Browser: "+h.browser);
}
}
When I run the program,it gives me different output all the time. i.e.
1:
Browser: firefox
Browser: firefox
2:
Browser: chrome
Browser: firefox
3:
Browser: chrome
Browser: chrome
4:
Browser: firefox
Browser: chrome
Can someone please suggest me the solution so that I will get consistent result

parallel=true executes all tests at a time(in parallel) and hence whichever test it gets first is executed first.So, test order is not maintained and hence different outputs every time.
Make parallel = none, and it will follow the order as mentioned in testng xml.
<suite name="MultiBrowsreTest" parallel="none">

Related

Running feature cucumber in parallel

I want to run a cucumber feature in different browsers;
So, now I'm able to open the 3 browsers in parallel chrome, ff and ie but they can't continue the other steps in features !
My method is :
#Parameters("myBrowser")
#BeforeClass
#Given("^openaaaBrowser<myBrowser>$")
public void openaaaBrowser(#Optional("optional value") String myBrowser) throws InterruptedException {
WebDriver driver;
if (myBrowser.equalsIgnoreCase("ie")) {
System.setProperty("webdriver.ie.driver","C:\\Driver\\IEDriverServer\\IEDriverServer_32bits.exe");
driver = new InternetExplorerDriver();
}
if (myBrowser.equalsIgnoreCase("chrome")) {
System.setProperty("webdriver.chrome.driver","D:\\Drive\\chromedriver_win32\\chromedriver.exe");
driver= new ChromeDriver();
}
if (myBrowser.equalsIgnoreCase("firefox")){
System.setProperty("webdriver.gecko.driver","D:\\Drive\\geckodriver-v0.20.0-win64\\geckodriver.exe");
driver = new FirefoxDriver();
}}
My testng.xml :
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="SuiteSopraHR" parallel="tests">
<test name="testff">
<parameter name="myBrowser" value="firefox" />
<classes>
<class name="com.soprahr.foryou.automation.steps.StepDefinitionConnect"/>
</classes>
</test> <!-- Test -->
<test name="testie">
<parameter name="myBrowser" value="ie" />
<classes>
<class name="com.soprahr.foryou.automation.steps.StepDefinitionConnect"/>
</classes>
</test> <!-- Test -->
<test name="testchrome">
<parameter name="myBrowser" value="chrome" />
<classes>
<class name="com.soprahr.foryou.automation.steps.StepDefinitionConnect"/>
</classes>
</test> <!-- Test -->
</suite> <!-- Suite -->
and I have those methods
#Test(priority=1)
#When("^Open browser$")
public void openBrowser() throws InterruptedException {
StepDefinition.DRIVER.get(URL);
Thread.sleep(N_3000);
StepDefinition.waitForJQueryProcessing(StepDefinition.DRIVER, N_30);
}
#Test(priority=2)
#Then("^Se connecter à l'environnement via ID '(.*)'$")
public void letThisOneConnect(final String Id) throws Throwable {
Thread.sleep(N_3000);
Utilities utilities = new Utilities();
TestCase testCase = utilities.getMyTestCase(Id);
StepDefinition.deleteAndEnterTextById(ID_LOGIN_INPUT_4YOU, testCase.getLogInId());
StepDefinition.deleteAndEnterTextById(ID_PASSWORD_INPUT_4YOU, testCase.getLogInPassword());
StepDefinition.clickButtonById(ID_LOGIN_BUTTON_4OU);
}
The problem here and I don't understand why it can't the #test methods
If you want to run a scenario with different browsers you have to run the scenario multiple times. i.e. if you have 3 browsers then you end up with 3 scenario instances.
You can't do is run one scenario in 3 browsers.
The simplest way to get your parallelism do this to take it out of Cucumber. If you ran in series you might have
cucumber features/my_feature BROWSER=chrome
cucumber features/my_feature BROWSER=firefox
cucumber features/my_feature BROWSER=ie
Now you could use your CI platform to run each of these commands in a separate instance. Then you'll get your parallelism, and all you have to do with Cucumber is get it to use an environment variable to control which driver and browser to use.
You won't succeed in getting Cucumber to work with more than one browser for a particular scenario instance.

How to run test cases in different Java classes in a new window every time using TestNG and Selenium

I have 15 test methods in 3 Java classes (Selenium Script). I want to run each Test class with new window. I am using TestNg framework.
Here is the code of TestNG:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Fanfight Test" thread-count="10" parallel="methods">
<listeners>
<listener class-name="com.fanfight.test_case.ListenerClass">
</listener>
</listeners>
<test name="User Login" parallel="false">
<classes>
<class name="com.fanfight.test_case.UserLogin"></class>
</classes>
</test>
<test name="Contest Creation" parallel="false" >
<classes>
<class name="com.fanfight.test_case.ContestCreation"></class>
</classes>
</test>
<test name="User Profile Test" parallel="false" >
<classes>
<class name="com.fanfight.test_case.UserProfileTest"></class>
</classes>
</test>
<test name="Menu Bar Test" parallel="false">
<classes>
<class name="com.fanfight.test_case.MenuBarTest">
</class>
</classes>
</test>
<test name="Home Page Elements" parallel="false" >
<classes>
<class name="com.fanfight.test_case.HomePageElementTest"></class>
</classes>
</test>
</suite>
Without using parallel="false" my script is running in alphabetical order due to which selenium unable to find the path and execution got stuck.
Also please suggest how to make execution continue even after getting an exception during execution.
Add a setup and tear down method in each of the 3 test classes. The setup method should launch the browser and teardown method should close that browser instance.
class TestOne {
WebDriver driver;
#BeforeClass
public void setup(){
driver = new ChromeDriver();
}
#Test
public void testCase1(){
}
//.... Other test methods
#AfterClass
public void tearDown(){
driver.quit();
}
You can also create a parent class having just the setup and tear down methods , pseudo coded above. All 3 of your test class shall extend this parent class. It will be an optimised approach as the driver instantiation and destruction is now centralised to a single class.
And finally , change the parallel attribute in the suite tag of your testNG xml, in order to make them run parallel.
<suite name="Fanfight Test" thread-count="10" parallel="classes">

Is it posible to run feature cucumber in parallel in different browser

I'm working in a big project, i want to run eature cucumber in parallel in different browser
I have the featuren the step definition ? the webdriverfactory and the shared preferences.
I have this method in webfactory and it works and i write the testng.xml
public WebDriver driver;
public static WebDriver get() {
WebDriver driver = null ;
System.setProperty("webdriver.chrome.driver","D:\\Drive\\chromedriver_win32\\chromedriver.exe");
driver= new ChromeDriver();
return(driver);
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="SuiteSopraHR" parallel="tests">
<test name="testie">
<!-- <parameter name="myBrowser" value="ie" /> -->
<classes>
<class name="com.driver.WebDriverFactory"/>
</classes>
</test> <!-- Test -->
<test name="testchrome">
<!-- <parameter name="myBrowser" value="chrome" /> -->
<classes>
<class name="com.driver.WebDriverFactory"/>
</classes>
</test> <!-- Test -->
</suite> <!-- Suite -->
I don't know how to change the other method because it didn't has any parameter to pass and it return a web driver.
when I have changed all the other method in other classes have a problem with it
any suggestion please.
and is the cucumber-jvm can run feature in parallel in different browser ??? or in console ???
You can indeed run Cucumber features and scenarios in parallel using Courgette-JVM
When you run your tests, you can set a System property that would target the browser you wish to use in parallel.
Another useful library to manage your driver binaries is WebDriver Binary Downloader
You can then specify the browser to use at runtime using:
System.setProperty("browser", "chrome");
or
VM option -Dbrowser="chrome"
private WebDriver driver;
public void createDriver() {
final String browser = System.getProperty("browser", "chrome").toLowerCase();
switch (browser) {
case "chrome":
WebDriverBinaryDownloader.create().downloadLatestBinaryAndConfigure(BrowserType.CHROME);
driver = new ChromeDriver();
case "firefox":
WebDriverBinaryDownloader.create().downloadLatestBinaryAndConfigure(BrowserType.FIREFOX);
driver = new FirefoxDriver();
default:
throw new RuntimeException("Invalid browser specified!");
}
}
We are using QAF-Gherkin-client, where you can configure it using one or more xml test nodes. You can run scenarios in parallel as well. You don't need to write any code for driver management or other functional testing common needs.
<suite name="AUT Test Automation" verbose="0" parallel="methods">
<test name="Tests on chrome">
<parameter name="driver.name" value="chromeDriver"/>
<classes>
<class name="com.qmetry.qaf.automation.step.client.gherkin.GherkinScenarioFactory" />
</classes>
</test>
<test name="Tests FF">
<parameter name="driver.name" value="firefoxDriver"/>
<classes>
<class name="com.qmetry.qaf.automation.step.client.gherkin.GherkinScenarioFactory" />
</classes>
</test>
</suite>
I think, you need to add switch construction in your method and parameter - type of browser from testng.xml.
Also, as I know, parallel execution will work only with a non-static Driver.

How to create testng.xml with following requirements?

Run tests in three browsers (chrome, firefox and ie) parallely. Each browser should open 2 instances. In total, on triggering testng.xml , 6 browser instances should be opened.
<suite thread-count=3 parallel="tests">
<test>
for firefox
</test>
<test>
for chrome
</test>
<test>
for ie
</test>
</suite>
Please help me!
In your TestNG.xml file , add a parameter for specifying browser type.
<test>
<parameter name="browser" value="firefox">
<parameter name="username" value="testuser"/>
<parameter name="password" value="testpassword"/>
<classes>
<class name="com.parameterization.TestParameters" />
</classes>
</test>
<test>
<parameter name="browser" value="chrome">
<parameter name="username" value="testuser"/>
<parameter name="password" value="testpassword"/>
<classes>
<class name="com.parameterization.TestParameters" />
</classes>
</test>
<test>
<parameter name="browser" value="ie">
<parameter name="username" value="testuser"/>
<parameter name="password" value="testpassword"/>
<classes>
<class name="com.parameterization.TestParameters" />
</classes>
</test>
</suite>
In your test class receive these parameters and create a webdriver according to the desired capabilities.
package com.parameterization;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
public class TestParameters {
#Parameters({ "browser", "username", "password" })
#Test
public void testCaseOne(String browser,String username, String password) {
System.out.println("browser passed as :- " + browser);
createWebDriver(browser);
loginToApplicationOne(username,password);
}
#Parameters({ "browser", "username", "password" })
#Test
public void testCaseTwo(String browser, String username, String password) {
createWebDriver(browser);
loginToApplicationTwo(username,password);
}
}
As you have set thread-count to 3 and your requirement is to launch 2 browser instances in each test block. You have to refactor your test classes in the above style so that each method block create its isolated driver instance. Thus a total of 6 browsers would be launched.
There is no clean way to do this through xml just workarounds.
You can add the invocationCount to the #Test annotation for the test you want to repeat. Refer to link for more details.
Also you could create a duplicate of the xml file and run them as parallel suites using -suitethreadpoolsize as argument. Also pass in both the xml files as argument. Refer to link for doc.
Plus as suggested in the answers before, copying the tests multiple times in same xml.

Is the parallel execution possible, without selenium grid?or (only testNG is enough?)

I am beginner in selenium webdriver. so for parallel execution normally we do changes in xml file like parallel="methods" thread-count="3" and my doubt is:
Is the parallel execution possible without selenium grid?
or only testNG is enough?
Yes, you may run UI tests in parallel without grid, or using only selenium grid node directly, without hub. Each thread in TestNG will open additional browser window, but you will get unpredictable issues in case when you application will manage all connections from you host as one user session.
Yes, using #Parameters ("browser") of TestNg..sample code as below...
#Parameters ("browser")
public void test(String browserName) {
if(browserName.equalsIgnoreCase("firefox")){
driver = new FirefoxDriver();
} else if (browserName.equalsIgnoreCase("chrome")){
System.setProperty("webdriver.chrome.driver", System.getProperty("user.dir")+"\\chromedriver.exe" );
driver = new ChromeDriver();
}
}
write your test after that in your testng.xml use parallel option also mention the parameter value.. sample code as below..
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite789787" parallel="tests">
<test name="FFTest98798">
<parameter name="browser" value="firefox"/>
<classes>
<class name ="crossbrowsertest.VerifyTitle" >
</class>
</classes>
</test> <!-- Test -->
<test name="ChromeTest8999">
<parameter name="browser" value="chrome"/>
<classes>
<class name ="crossbrowsertest.VerifyTitle" >
</class>
</classes>
</test> <!-- Test -->
</suite> <!-- Suite -->
hope this helps