Selenium Configuration Failures while running TestNG - selenium

In selenium why does the result
Default test
Tests run: 1, Failures: 0, Skips: 1
Configuration Failures: 1, Skips: 0 comes?
Why is this so. I was trying to run a test.
public class NewTest {
WebDriver driver;
#BeforeTest
#Parameters({"browser"})
public void setup(#Optional String browser) throws Exception{
if(browser.equalsIgnoreCase("Firefox")){
driver = new FirefoxDriver();
}
else if(browser.equalsIgnoreCase("Chrome")){
System.setProperty("webdriver.chrome.driver","path");
driver = new ChromeDriver();
}
else if(browser.equalsIgnoreCase("IE")){
System.setProperty("webdriver.ie.driver","path");
driver = new InternetExplorerDriver();
}
else{
throw new Exception("Browser is not correct");
}
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
}
#Test
public void testParameterWithXML() throws InterruptedException{
driver.get("http://demo.guru99.com/V4/");
WebElement userName = driver.findElement(By.name("uid"));
userName.sendKeys("guru99");
WebElement password = driver.findElement(By.name("password"));
password.sendKeys("guru99");
}}
This is my code. I was trying to run this, but getting errors.
FAILED CONFIGURATION: #BeforeTest setup(null)
java.lang.NullPointerException
at zproject.NewTest.setup(NewTest.java:21)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:104)
at org.testng.internal.Invoker.invokeConfigurationMethod(Invoker.java:515)
at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:217)
at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:144)
at org.testng.TestRunner.beforeRun(TestRunner.java:634)
at org.testng.TestRunner.run(TestRunner.java:602)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:387)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:382)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:340)
at org.testng.SuiteRunner.run(SuiteRunner.java:289)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1293)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1218)
at org.testng.TestNG.runSuites(TestNG.java:1133)
at org.testng.TestNG.run(TestNG.java:1104)
at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:132)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:236)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:81)
Also
Default test
Tests run: 1, Failures: 0, Skips: 1
Configuration Failures: 1, Skips: 0
suite.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "testng.org/testng-1.0.dtd">;
<suite name="TestSuite" thread-count="2" parallel="tests" >
<parameter name="browser" value="Chrome" />
<test name="ChromeTest">
<classes>
<class name="zproject.NewTest"></class>
</classes>
</test>
<parameter name="browser" value="Firefox" />
<test name="FirefoxTest">
<classes>
<class name="zproject.NewTest"></class>
</classes>
</test>
<parameter name="browser" value="IE" />
<test name="IETest">
<classes>
<class name="zproject.NewTest"> </class>
</classes>
</test>
</suite>

#Optional means the value will be null if you don't configure it.
In your case, I think you didn't and then the value of browser is null and produces an NPE.
As you throw an exception in the case of a bad browser, just remove the #Optional and TestNG will fail as expected.
Next, you'll need a suite.xml:
<suite name="My suite">
<parameter name="browser" value="Firefox"/>
<test name="Simple example">
<classes>
<class name="NewTest"/>
</classes>
</test>
</suite>
Have a look on the parameter part of the documentation too.

Related

Extent pdf report is empty when executing TestNG.xml that has more than 1 test

When I execute my testNG.xml that has more than one test then its showing an Exception - 'The TrueType font null does not contain a 'cmap' table' from the second test and the Extent report.pdf could not be opened because it is empty.
 I see that the cucumber report for each test on console and Spart report for all tests are generated. Can you pls help me on how to generate extent pdf's when executing multiple tests via testNG?
When the testNG has single test, then I see that the extent report.pdf is generated nicely.
TestNG:
<suite name="Feature Test Suite" verbose="1" data-provider-thread-count="5" configfailurepolicy="continue" parallel="true">
<test name="Firefox" annotations="JDK" preserve-order="true">
<parameter name="browser" value="firefox"></parameter>
<classes>
<class name="cucumberOptions.ProjectsTestRunner"></class>
</classes>
</test>
<test name="Chrome" annotations="JDK" preserve-order="true">
<parameter name="browser" value="chrome"></parameter>
<classes>
<class name="cucumberOptions.ProjectsTestRunner"></class>
</classes>
</test>
</suite>
Hooks:
#Before
public void EnvDetails(Scenario scenario) throws IOException {
testContextSetup.testBase.WebDriverManager();
testContextSetup.testBase.setScenario(scenario);
Capabilities cap = ((RemoteWebDriver) testContextSetup.testBase.WebDriverManager()).getCapabilities();
String brName = cap.getBrowserName().toUpperCase();
ExtentCucumberAdapter.getCurrentScenario().assignCategory(brName+ " - " + System.getProperty("os.name"));
scenario.log("Environment: "+brName+ " " + cap.getBrowserVersion()+ " in " + System.getProperty("os.name"));
scenario.log("Executed By: " + System.getProperty("user.name").toUpperCase());
}
#After
public void AfterMethod() throws IOException {
testContextSetup.testBase.WebDriverManager().quit();
}

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.

How To Run Cross Browser Testing - Selenium Tests On BrowserStack

I am using page object model, I want to run cross-browser testing on browser stack. I am stuck at BeforeTest method, bcoz of dataProvider does not use with BeforeTest.
public static void setup() throws MalformedURLException {
browserStack();
}
#Test (dataProvider = "browserStackData")
public static void browserStack(Platform platform,String browserName,String browserVersion) throws MalformedURLException {
DesiredCapabilities caps = new DesiredCapabilities();
caps.setPlatform(platform);
caps.setBrowserName(browserName);
caps.setBrowserName(browserVersion);
caps.setCapability("project", "WebAPP");
caps.setCapability("build", "1.0");
caps.setCapability("name", "Login");
caps.setCapability("browserstack.local", "false");
caps.setCapability("browserstack.networkLogs", "true");
driver = new RemoteWebDriver(new URL(URL), caps);
}
#DataProvider(name = "browserStackData" , parallel = true)
public Object[][]getData() {
Object[][] testData = new Object[][]{
{Platform.MAC, "chrome", "84"},
{Platform.WIN10, "firefox", "78"},
{Platform.MAC, "safari", "13.1"}
};
return testData;
}
In order to run tests on BrowserStack, you only need to change the Hub URL, if you are able to test locally then your test will run on BrowserStack too.
Steps to follow-
1) Specify the BrowserStack Hub URL as:
“https://” + USERNAME + “:” + AUTOMATE_KEY + “#hub-cloud.browserstack.com/wd/hub”;
2) Pass the Desired Capabilities in the test scripts as mentioned in the link: https://www.browserstack.com/automate/capabilities
You can refer to the documentation here: https://www.browserstack.com/docs?product=automate
Also, their GitHub repos should help you: https://github.com/browserstack
Answer
Reference to this link : https://www.browserstack.com/guide/how-to-setup-browserstack-automate
testng.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite thread-count="2" name="Suite" verbose="2" parallel="tests">
<test name="Test on Chrome">
<parameter name="browser" value="Chrome"/>
<parameter name="browserVersion" value="83.0"/>
<parameter name="os" value="Windows"/>
<parameter name="osVersion" value="10"/>
<classes>
<class name="com.TC01Login"/>
<class name="com.TC02Dashboard"/>
</classes>
</test>
<test name="Test on Firefox">
<parameter name="browser" value="Chrome"/>
<parameter name="browserVersion" value="83.0"/>
<parameter name="os" value="Windows"/>
<parameter name="osVersion" value="10"/>
<classes>
<class name="com.TC01Login"/>
<class name="com.TC02Dashboard"/>
</classes>
</test>
<test name="Test on Safari">
<parameter name="browser" value="Safari"/>
<parameter name="browserVersion" value="13.0"/>
<parameter name="os" value="OS X"/>
<parameter name="osVersion" value="Catalina"/>
<classes>
<class name="com.TC01Login"/>
<class name="com.TC02Dashboard"/>
</classes>
</test>
</suite>
#Parameters({"browser","browserVersion","os","osVersion"})
#BeforeTest
public static void browserStack(String browser, String browserVersion, String os,String osVersion) throws MalformedURLException {
DesiredCapabilities caps = new DesiredCapabilities();
caps.setCapability("browser",browser);
caps.setCapability("browserVersion",browserVersion);
caps.setCapability("os",os);
caps.setCapability("osVersion",osVersion);
caps.setCapability("project", "xyz");
caps.setCapability("build", "1.0");
caps.setCapability("name", "Login");
caps.setCapability("browserstack.local", "false");
caps.setCapability("browserstack.networkLogs", "true");
caps.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
driver = new RemoteWebDriver(new URL(URL), caps);
}

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">

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.