Right now, I'm trying to work on my first test that will test browsers Chrome, Firefox, IE, and Safari in parallel. But the following error that I'm getting is this:
FAILED CONFIGURATION: #BeforeMethod beforeMethod
org.testng.TestNGException:
Parameter 'browser' is required by #Configuration on method beforeMethod but >has not been marked #Optional or defined
I'm using Selenium, TestNG and Maven with the JAVA language. The XML test suite file and the java file are in the same folder in the directory. What I was able to find for the test suite XML file online is as follows (with class name values set to the correct package and class name):
<?xml version="1.0" encoding="UTF-8"?>
<DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="TestSuite" thread-count="2" parallel="tests">
<test name="ChromeTest">
<parameter name="browser" value="chrome" />
<classes>
<class name="com.sqa.ts.multiBrowser.BrowserTest">
</class>
</classes>
</test>
<test name="FirefoxTest">
<parameter name="browser" value="firefox" />
<classes>
<class name="com.sqa.ts.multiBrowser.BrowserTest">
</class>
</classes>
</test>
<test name="IETest">
<parameter name="browser" value="ie" />
<classes>
<class name="com.sqa.ts.multiBrowser.BrowserTest">
</class>
</classes>
</test>
<test name="SafariTest">
<parameter name="browser" value="safari" />
<classes>
<class name="com.sqa.ts.multiBrowser.BrowserTest">
</class>
</classes>
</test>
</suite>
Below is my code to just open the browser to make sure that it will run and pass:
package com.sqa.ts.multiBrowser;
import java.net.MalformedURLException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.safari.SafariDriver;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
public class BrowserTest {
private WebDriver driver;
#Test
public void testCaseOne() {
driver.get("http://www.google.com");
driver.close();
}
#BeforeMethod
#Parameters("browser")
public void beforeMethod(String browser) throws MalformedURLException {
if (browser.equalsIgnoreCase("chrome")) {
System.setProperty("webdriver.chrome.driver", "C:/Users/Trevor/workspace/BrowserTest/drivers/chromedriver.exe");
driver = new ChromeDriver();
} else if (browser.equalsIgnoreCase("firefox")) {
driver = new FirefoxDriver();
} else if (browser.equalsIgnoreCase("ie")) {
System.setProperty("webdriver.ie.driver", "C:/Users/Trevor/workspace/BrowserTest/drivers/IEDriverServer.exe");
driver = new InternetExplorerDriver();
} else if (browser.equalsIgnoreCase("safari")) {
driver = new SafariDriver();
}
}
#AfterMethod
public void afterMethod() {
driver.quit();
}
}
If anyone can give me an insight as to what is causing this issue, I would greatly appreciate it. Thank you.
Looks like you are missing the testNG configuration in the pom.xml
<build>
<plugins>
<!-- Following plugin executes the testng tests -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19</version>
<configuration>
<!-- Suite testng xml file to consider for test execution -->
<suiteXmlFiles>
<suiteXmlFile>src/test/java/com/sqa/ts/multiBrowser/testng.xml</suiteXmlFile>
</suiteXmlFiles>
</configuration>
</plugin>
</build>
Running mvn clean install or mvn clean install should run the test cases. Hope this helps you.
It looks like executing the XML file as Run as > Testng Suite. That was the fix to my problem.
It worked with option = Run as > Testng Suite
Related
I have testNG file which has multiple methods.
In this testNG file I added the parameters to pass the parameter on code from xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="LocalIosTestSuite" parallel="tests" thread-count="1" >
<test name="iphonetest">
<parameter name="platformName" value="iphonetest" />
<classes>
<class name="tests.TestKotlin">
</class>
</classes>
</test>
<test name="androidtest">
<parameter name="platformName" value="androidtest" />
<classes>
<class name="tests.TestKotlin">
</class>
</classes>
</test>
</suite>
Kotlin Class
import org.testng.annotations.Parameters
import org.testng.annotations.Test
class TestKotlin {
#Parameters("platformName")
#Test()
fun Testmethod(platformName:String)
{
println("TestKotlin")
println(platformName)
}
}
Command to run a single test from testng file
./gradlew test -Psuite5 -testnames iphonetest
This is not working for me please help me any solution to run a specific test on gradle
I have a simple maven project with three class files.
When I run my testng.xml file, tests are running in series.
I tried keeping parallel="classes", parallel="methods", parallel="tests" but no luck.
Also tried by changing testng version from 6.9.0 to 7.0.0 but it didn't work.
Expectation:
Tests should run in parallel
What is happening:
Tests run in sequence
Below is my project and all files:
Test Class: 1
public class TestOne {
private static WebDriver driver;
private static String baseURL;
#Test
public void launch() throws IOException {
baseURL = "http://www.gmail.com";
System.setProperty("webdriver.chrome.driver", "path");
WebDriver driver = new ChromeDriver();
driver.get(baseURL);
driver.quit();
}
}
Test Class: 2
public class TestTwo {
private static WebDriver driver;
private static String baseURL;
#Test
public void launch() throws IOException {
baseURL = "http://www.gmail.com";
System.setProperty("webdriver.chrome.driver", "path");
WebDriver driver = new ChromeDriver();
driver.get(baseURL);
driver.quit();
}
}
Test Class: 3
public class TestThree {
private static WebDriver driver;
private static String baseURL;
#Test
public void launch() throws IOException {
baseURL = "http://www.gmail.com";
System.setProperty("webdriver.chrome.driver", "path");
WebDriver driver = new ChromeDriver();
driver.get(baseURL);
driver.quit();
}
}
TestNG xml:
!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd"
suite name="Test" parallel="tests" thread-count="3" data-provider-thread-count="5"
<test name="Launch">
<classes>
<class name="test.demo.grid.TestOne" />
<class name="test.demo.grid.TestTwo" />
<class name="test.demo.grid.TestThree" />
</classes>
</test>
Version:
selenium-java = 3.4.0
testng = 6.14.3
=================================
Please let me know how I can fix it.
Thank you!
Try first a simple example logging the thread id on each test method, without calling ChromeDriver yet
long id = Thread.currentThread().getId();
System.out.println("Sample test-method " + testName
+ ". Thread id is: " + id);
https://howtodoinjava.com/testng/testng-executing-parallel-tests/
If it logged the ids in parallel maybe is something related to ChromeDriver
<test> tag is only one in your testng.xml and that is the reason, parallel execution is not working.
In your case, you need to use parallel="classes" and all the classes would execute parallely.
Edit:
You can use the testng.xml as:
<test name="Launch">
<classes>
<class name="test.demo.grid.TestOne" />
</classes>
</test>
<test name="Second test">
<classes>
<class name="test.demo.grid.TestTwo" />
</classes>
</test>
<test name="Third test">
<classes>
<class name="test.demo.grid.TestThree" />
</classes>
</test>
And now use parallel="tests" in the testng.xml
Try with parallel="classes" and without the data-provider-thread-count="5" because you are not using data provider here.
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Test" parallel="classes" thread-count="3">
<test name="Launch">
<classes>
<class name="test.demo.grid.TestOne" />
<class name="test.demo.grid.TestTwo" />
<class name="test.demo.grid.TestThree" />
</classes>
</test>
</suite>
You can try:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite" configfailurepolicy="continue">
<parameter name="parallelSetup" value="tests" />
<parameter name="threadsNumber" value="3" />
<test name="Launch">
<classes>
<class name="test.demo.grid.TestOne" />
<class name="test.demo.grid.TestTwo" />
<class name="test.demo.grid.TestThree" />
</classes>
</test>
</suite>
add #BeforeClass annotation in every class and initialize WebDriver in that annotaion
and close driver in #AfterClass annotion for every class
I am not able to invoke the browsers in parallel, which are currently invoking one after another. Need a way to invoke the browsers in parallel tests.
NOTE: In my configuration xml file I have kept the thread count as 2.
Below is my configuration file:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "testng.org/testng-1.0.dtd"; >
<suite name="Parallel" parallel="tests" thread-count="4" >
<test verbose="3" name="<name>">
<parameter name="platform" value ="win8"/>
<parameter name="browsername" value ="internet explorer"/>
<classes>
<class name="com.parallel.execution.ParallelExecution">
<methods>
<include name="testmethod1"/>
</methods>
</class>
</classes>
</test>
</suite>
We have to define two attributes 'parallel' and 'thread-count' in simple testng.xml file. See below:
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Parallel Execution suite" parallel="methods" thread-count="2">
<test name="Regression 2">
<classes>
<class name="com.parallel.TestParallelExecution"/>
</classes>
</test>
</suite>
In above, as we want the test methods to be executed parallel, we have set the parallel attribute as 'methods' and thread-count attribute will control the max number of threads to be created.
Re-installing TestNG solved the above problem.
You have to explicitly write code to invoke the browser as part of a #Before configuration to invoke the browser every time a #Test is run . I will specify one of the many approaches so that you get an idea.
<suite name="Parallel" parallel="tests" thread-count="4" >
<test verbose="3" name="test1">
<classes>
<class name="com.parallel.execution.ParallelExecution1"/>
</classes>
</test>
<test verbose="3" name="test2">
<classes>
<class name="com.parallel.execution.ParallelExecution2"/>
</classes>
</test>
</suite>
Consider a suite file with 2 tests set to run in parallel. What we expect is #Test methods in ParallelExecution1 runs in first browser and #Test methods in ParallelExecution2 runs in second browser. So you need a mechanism by which you can invoke browser sessions and run your test methods. Enter BaseTest class.
public abstract class BaseTest {
protected WebDriver driver;
#BeforeTest
#Parameters({"browser"})
public void init(String browser) {
// Initialize your browser here. Code below is dummy
driver = new FF();
}
#AfterTest
public void end() {
driver.close();
driver.quit();
}
}
Now inherit this 'BaseTest' in both your test classes.
public class ParallelExecution1 extends BaseTest {
#Test
public void test1() {
}
}
public class ParallelExecution2 extends BaseTest {
#Test
public void test2() {
}
}
Now both the tests have #BeforeTest and #AfterTest methods which will invoke the browsers.
Why is my TestNG xml file not targeting my Runner Class?
My XML File:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="Cucumber Framework" verbose="2">
<test name="Cucumber Tests">
<classes>
<class name="runners.MainRunner"></class>
</classes>
</test>
</suite>
location of the runner class:
CucumberFramework\src\test\java\CucumberFramework\runners
CucumberFramework\src\test\java\CucumberFramework\runners\MainRunner.java
Please note: if i execute the runner class directly, everything works accordingly, it seems the xml file is unable to locate the runner class, any ideas?
Runner class code:
package CucumberFramework.runners;
import org.junit.runner.RunWith;
import cucumber.api.CucumberOptions;
import cucumber.api.junit.Cucumber;
#RunWith(Cucumber.class)
#CucumberOptions (
features = {"src/test/java/CucumberFramework/featureFiles/"},
glue = {"CucumberFramework.steps"},
monochrome = true,
tags = {},
plugin = {"pretty", "html:target/cucumber","json:target/cucumber.json", "com.cucumber.listener.ExtentCucumberFormatter:target/report.html"}
)
public class MainRunner {
}
You can execute the junit classes with testng xml file by define the property junit="true" as given below
<suite name="Cucumber Framework" >
<test name="Cucumber Tests" junit="true">
<classes>
<class name="CucumberFramework.runners.MainRunner" />
</classes>
</test>
</suite>
I have created a Selenium project named 'LearnAutomation' in Java and convert it to TestNG tests which created a testng.xml for me. Now i want to run it via batch file. So i created one as below -
cd E:\Workspace - Eclipse\LearnAutomation
set ProjectPath=E:\Workspace - Eclipse\LearnAutomation
echo %ProjectPath%
set classpath=%ProjectPath%\bin;%ProjectPath%\lib\*
echo %classpath%
java org.testng.TestNG %ProjectPath%\testng.xml
and save it as testng.bat file. On double clicking batch file, it gets launched and disappeared. (failed to run test)
My testng.xml looks like this -
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite" parallel="none">
<test name="Test">
<classes>
<class name="tests.Demo"/>
</classes>
</test> <!-- Test -->
</suite> <!-- Suite -->
My Demo class looks like this -
package tests;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.Test;
public class Demo {
#Test
public void batchFile() {
System.setProperty("webdriver.gecko.driver",
"C:\\Users\\srvashishtha\\Downloads\\geckodriver-v0.11.1-win64\\geckodriver.exe");
WebDriver driver = new FirefoxDriver();
driver.get("http://www.google.com");
}
}