will testng initiate as many webdriver instance as test cases? - selenium

I am facing a problem of Selenium Grid and TestNG. My test scripts will NOT quite the browser until all the test class are executed.
I've configed 5 Chrome instance running on my Selenium Grid, then below is my xml file
<suite name="BrowserTest">
<test name="Test1" >
<parameter name="browserName" value="chrome"/>
<classes>
<class name= "Pagetest" />
</classes>
</test>
<test name="Test2" >
<parameter name="browserName" value="firefox"/>
<classes>
<class name= "Pagetest" />
</classes>
</test>
<test name="Overview Page Test on ie, English" >
<parameter name="browserName" value="ie"/>
<classes>
<class name= "Pagetest" />
</classes>
</test>
</suite>
Here is my test scripts
public class Pagetest {
private WebDriver browser;
public PateTest( String browser){
// create a browser instance.
}
#AfterClass (or #afterTest)
public void afterTest() throws Exception {
System.out.println("this test is done");
this.browser.quit();
}
#Test
public void test1(){
this.browser.get("www.google.com");
// doing some login stuff
}
}
Now when I am running my test, testNG will create 3 chrome webdirver instance one by one (with running the test). but after the 1st test is done, the browser instance of 1st test is not quited until all 3 tests are done, then all the 3 chrome instance are quited almost at the same time. What I want is after the 1st test is done, it's browser instance is quited, then the 2nd test case is started, did I do something wrong or how should I change my code?

Try using threadlocal it would help when you are even running tests in parallel
public ThreadLocal<WebDriver> driver=null;
#beformethod()
public void setUp
{
driver = new ThreadLocal<WebDriver>();
driver.set(new ChromeDriver());
return driver.get();
}
#AfterMethod
public void closeBrowser() {
{
driver.get().quit();
}

Related

How to launch & use separate browser for each test method inside a class

I have the below scenario
All the 3 tests run, but they are sharing only 1 browser. I have all the common methods for all the clicks etc in a Base class.
I want each of the test method -method1/2/3 to launch different browser & work,
Can somebody help?
Class A extends BaseTest{
#BeforeMethod(){
initDriver();// it does setdriver & getDriver is used across
}
public void doStuff(){
...
}
#Test
public void method1(){
doStuff()
}
#Test
public void method2(){
doStuff()
}
#Test
public void method3(){
doStuff()
}
}
You can use TestNG.xml to parameterized and control your execution as below. Here we ahve created 3 different tests , each one of them is parameterized with browser type variable and running one of your #test method.
TestNG.XML
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite">
<test name="TestChrome">
<parameter name="browser" value="chrome"/>
<classes>
<class name ="<Full path to your Test class>" />
<methods>
<include name="method1" />
</methods>
</classes>
</test>
<test name="TestForefox">
<parameter name="browser" value="firefox"/>
<classes>
<class name ="<Full path to your Test class>" />
<methods>
<include name="method2" />
</methods>
</classes>
</test>
<test name="TestIE">
<parameter name="browser" value="edge"/>
<classes>
<class name ="<Full path to your Test class>" />
<methods>
<include name="method3" />
</methods>
</classes>
</test>
</suite>
Get the browser type parameter in your #BeforeMethod.
#Parameters ({"browser"})
#BeforeMethod(){
initDriver(browser);/* it does setdriver & getDriver is used across. Passing browser name
to initDriver method.*/
}
Now in initDriver() method ( wherever you have implemented it), set driver based on your browser type. Something similar to below:
public void initDriver(String browser) throws Exception{
if(browser.equalsIgnoreCase("firefox")){
System.setProperty("webdriver.gecko.driver", ".\\geckodriver.exe");
driver = new FirefoxDriver();
}
else if(browser.equalsIgnoreCase("chrome")){
System.setProperty("webdriver.chrome.driver",".\\chromedriver.exe");
driver = new ChromeDriver();
}
else if(browser.equalsIgnoreCase("Edge")){
System.setProperty("webdriver.edge.driver",".\\MicrosoftWebDriver.exe");
driver = new EdgeDriver();
}
else{
throw new Exception("Browser is not correct");
}
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.window().maximize();
}
Note: Using include/exclude tag in TestNg.xml file you can run/ignore any #Test methods inside a test.

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.

Mobile parallel execution with Selenium and Testng

I'm trying to execute one test script on two android devices simultaneously using Selenium and TestNG. But I'm unable to achieve it. The test script executes first on one device and then starts executing on other android device.Here is the TestNg.xml which i have used:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Parallel test suite" parallel="methods" thread-count="2">
<test name="Regression 1">
<parameter name="devname" value="adb:SM-G900H" />
<classes>
<class name="Parallel.EribankTetsng"/>
</classes>
</test>
<test name="Regression 2">
<parameter name="devname" value="adb:GT-I9300" />
<classes>
<class name="Parallel.EribankTetsng"/>
</classes>
</test>
</suite>
EribankTestng.java: The below is the test script which is export from seetest automation to selenium. we are connecting two android devices to same machine and trying to execute parallely using Testng. only after the execution is completed one device, the execution is getting started on the second device.
public class EribankTetsng
{
private String host = "localhost";
private int port = 8889;
private String projectBaseDirectory = "D:\\Vijaya Seetest";
protected Client client = null;
#BeforeTest
public void setUp(){
client = new Client(host, port, true);
client.setProjectBaseDirectory(projectBaseDirectory);
client.setReporter("xml", "reports", "LoginEribank");
}
#Test(groups = {"seetest"})
#Parameters("devname")
public void testLoginEribank(String devname){
client.setDevice(devname);
client.launch("com.experitest.ExperiBank/.LoginActivity", true, false);
client.elementSendText("Eribank", "Username", 0, "company");
client.elementSendText("Eribank", "passwordTextField", 0, "company");
client.click("Eribank", "Login", 0, 1);
client.click("Eribank", "Logout", 0, 1);
}
#AfterTest
public void tearDown(){
client.generateReport(false);
client.releaseClient();
}
}

How to use testNG #Parameters in #BeforeSuite to read value from testng.xml

I am using testNG with Selenium webdriver 3.4. I want to perform the test on for different browser at the same time.
In my testNG.xml I have
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Demo">
<test name="Demo on Chrome">
<parameter name="browser" value="Chrome" />
<classes>
<class name="catalogueMemberPortal.CatalogueTest"/>
</classes>
</test>
<test name="Demo on Firefox">
<parameter name="browser" value="Firefox" />
<classes>
<class name="catalogueMemberPortal.CatalogueTest"/>
</classes>
</test>
</suite>
My Code
public class BaseClass {
public static WebDriver driver;
public WebDriver getDriver() {
return driver;
}
#BeforeSuite
#Parameters({"browser"})
public void launchBrowser(String browser) throws Exception {
if (browser.equalsIgnoreCase("Firefox")){
String ffDriverPath = ((System.getProperty("user.dir")+"/browser_drivers/firefox/geckodriver.exe")) ;
System.setProperty("webdriver.gecko.driver",ffDriverPath);
driver = new FirefoxDriver();
logger.info("Firefox Initialized");
}
// same goes for other type of browsers
#AfterSuite
public void afterSuite() {
driver.close();
driver.quit();
}
}
Error I am getting
org.testng.TestNGException:
Parameter 'browser' is required by BeforeSuite on method launchBrowser but has not been marked #Optional or defined
in C:\testng.xml
Your browser parameter is located into the <test> node, not in the <suite> node.
At the suite level, the parameter doesn't exist.
Just replace #BeforeSuite and #AfterSuite by #BeforeTest and #AfterTest.

How to run parallel Test case in Selenium + TestNG

I have a test script as follows, what I need to know is how I can make it to run in different browser at same time but with different test data,
Exactly what I want is I want parallel instance of Test Case one in Firefox and other in Internet Explorer, but again I want to use different data for it
Eg:
-Firefox with 'username1' and 'password1'
-Internet Explorer 'username2' and 'password2'
it is like logging into GMAIL with different usernames and password parallel in different browser but not in sequence.
Test Case :
public class Gmail
{
private WebDriver driver;
#BeforeClass
public void setup()
{
driver = new FirefoxDriver();
driver.get("http://www.gmail.com");
}
#Test
public void search()
{
WebElement element = driver.findElement(By.name("username"));
element.sendKeys("username");
WebElement element2 = driver.findElement(By.name("pass"));
element2.sendKeys("password");
element2.submit();
}
#Test
public void compose()
{
driver.findElement(By.name("compose")).click();
}
#AfterClass
public void exit()
{
driver.quit();
}
}
Please give your suggestion would help in my project.
Steps to do this:
Pass the required values as parameters in testNG.xml - in your case you need browserName, userName & password.
Create 2 separate "test" tag in testNG.xml with different userName & password parameter
<test name="Test on FF">
<parameter name="browserName" value="Firefox" />
<parameter name="userName" value="user1" />
<parameter name="password" value="pass1" />
<classes>
<class>name="Gmail"</class>
</classes>
</test>
<test name="Test on IE">
<parameter name="browserName" value="IE" />
<parameter name="userName" value="user2" />
<parameter name="password" value="pass2" />
<classes>
<class>name="Gmail"</class>
</classes>
</test>
Define "suite" tag as <suite thread-count="2" name="Suite" parallel="tests">
Access and use those in your #Test method by #Parameters ({"browserName","userName","password"}) and instantiate corresponding driver
If you have huge set of data, you can externalize it. Move it to an excel, yaml or whichever format you are comfortable with. Use that as an input to your #DataProvider method. In the #DataProvider, read all values from your chosen format and return. In your testmethods specify the dataprovider method. Read more about dataproviders here. Give the excel sheet in your parameter values. You can run the dataprovider parallelly by setting the
parallel to true. Make sure your driver instances are either Threadlocal values or are instantiated for each method else parallel runs may lead to failures.
I found the solution with working code on http://www.ufthelp.com/2014/12/Parallel-execution-tests-in-testNG-eclipse.html
XML File will look like this
<suite name="Suite" parallel="tests" thread-count="2">
<test name="Run in Firefox">
<parameter name="browser" value="firefox"></parameter>
<parameter name="userName" value="Test1"></parameter>
<parameter name="Password" value="Pwd1"></parameter>
<classes>
<class name="srcTest.ParallelTesting"/>
</classes>
</test>
<test name="Run in chrome">
<parameter name="browser" value="chrome"></parameter>
<parameter name="userName" value="Test2"></parameter>
<parameter name="Password" value="Pwd2"></parameter>
<classes>
<class name="srcTest.ParallelTesting"/>
</classes>
</test>
</suite>
Java Code:-
#Test
#Parameters({"userName","Password"})
public void login(String userName,String Password){
--Your Code--
}
#BeforeClass<br/>
#Parameters({"browser"})
public void BeforeClass(String browser) throws Exception {
--Your code--
}