iam new to selenium webdriver.
iam trying to run a Testng Test Parellel on two browsers but iam struck, getting the Following error. when trying to run.
package rough;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.Optional;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
import org.testng.annotations.BeforeMethod;
import org.testng.Assert;
import java.util.regex.Pattern;
import java.util.concurrent.TimeUnit;
import org.junit.*;
import static org.hamcrest.CoreMatchers.*;
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.support.ui.Select;
public class Browsers {
private WebDriver driver;
private String baseUrl;
private boolean acceptNextAlert = true;
private StringBuffer verificationErrors = new StringBuffer();
#BeforeMethod
public void tearDown1() throws Exception {
System.out.println("Hello starting");
}
#Parameters("BROWSER")
public void setUp(#Optional String BROWSER) throws Exception {
//To run test case parallely in different browsers
if(BROWSER.equalsIgnoreCase("FF"))
{
//System.out.println(“Firefox driver would be used”);
driver = new FirefoxDriver();
}
else
if(BROWSER.equalsIgnoreCase("IE"))
{
//System.out.println(“Ie webdriver would be used”);
System.setProperty("webdriver.ie.driver", "g:/Selenium Jar Files/IEDriverServer.exe");
driver = new InternetExplorerDriver();
}
// driver = new FirefoxDriver();
baseUrl = "http://book.theautomatedtester.co.uk/";
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
}
#Test
public void test() throws Exception {
driver.get(baseUrl + "/chapter1");
driver.findElement(By.id("radiobutton")).click();
new Select(driver.findElement(By.id("selecttype"))).selectByVisibleText("Selenium RC");
}
#AfterMethod
public void tearDown() throws Exception {
driver.quit();
String verificationErrorString = verificationErrors.toString();
if (!"".equals(verificationErrorString)) {
Assert.fail(verificationErrorString);
}
}
private boolean isElementPresent(By by) {
try {
driver.findElement(by);
return true;
} catch (NoSuchElementException e) {
return false;
}
}
private boolean isAlertPresent() {
try {
driver.switchTo().alert();
return true;
} catch (NoAlertPresentException e) {
return false;
}
}
private String closeAlertAndGetItsText() {
try {
Alert alert = driver.switchTo().alert();
String alertText = alert.getText();
if (acceptNextAlert) {
alert.accept();
} else {
alert.dismiss();
}
return alertText;
} finally {
acceptNextAlert = true;
}
}
}
below is the XML
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite" parallel="True">
<test name="Test">
<parameter name = "BROWSER" value="FF"></parameter>
<classes>
<class name="rough.Browsers"/>
</classes>
<test name="Test">
<parameter name = "BROWSER" value="IE"></parameter>
<classes>
<class name="rough.Browsers"/>
</classes>
</test> <!-- Test -->
</suite> <!-- Suite -->
Iam getting the stack trace
[TestNG] Running:
C:\Users\Administrator\AppData\Local\Temp\testng-eclipse--757511090\testng-customsuite.xml
Hello starting
FAILED CONFIGURATION: #AfterMethod tearDown
java.lang.NullPointerException
at rough.Browsers.tearDown(Browsers.java:65)
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:84)
at org.testng.internal.Invoker.invokeConfigurationMethod(Invoker.java:564)
at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:213)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:786)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:901)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1231)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:127)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:111)
at org.testng.TestRunner.privateRun(TestRunner.java:767)
at org.testng.TestRunner.run(TestRunner.java:617)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:334)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:329)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:291)
at org.testng.SuiteRunner.run(SuiteRunner.java:240)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1224)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1149)
at org.testng.TestNG.run(TestNG.java:1057)
at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:111)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:204)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:175)
Please help me
The real error is a NullPointerException. I'm going to guess that somewhere in your setup (or in your test), you throw an error, and then you try to access the driver and it is null.
Either put a null check before calling driver.close(), or make sure the driver is never null (I like the first option better).
I received the same error. My issue was that i tried to quite the driver inside #afterMethod. I closed the driver instead of quitting, and the problem got resolved.
I was facing the same problem and to solve it, I updated the chromedriver into my system. Actually one need to check the compatibility of browser and browser driver.
For example, if I was using Chrome 74 so I would update my driver to support Chrome 64.
Related
This is Parent Class to run the ChromeDriver.
package logInCredit;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class LogInPage {
public WebDriver driver ;
public void LogInCredit() {
//Open ChromeDriver
WebDriver driver= new ChromeDriver();
driver.get("https://opensource-demo.orangehrmlive.com/");
driver.manage().window().maximize();
System.out.println(driver);
}
}
This Is Child Class which takes the url from Parent Class
package afterLogIn;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.testng.annotations.Test;
import logInCredit.LogInPage;
public class NextLogIn extends LogInPage {
#Test
public void DashboardPage() {
System.out.println("driver is " +driver);
//Admin UserName Enter
WebElement AdminUserName =
driver.findElement(By.xpath("//input[#name='txtUsername']"));
AdminUserName.sendKeys("Admin");
//Admin Password Enter
WebElement AdminPassword =
driver.findElement(By.xpath("//input[#name='txtPassword']"));
AdminPassword.sendKeys("admin123");
//click Login
WebElement LogInButton = driver.findElement(By.xpath("//input[#id='btnLogin']"));
LogInButton.click();
WebElement ClickAdmin = driver.findElement(By.xpath("//*[text()='Admin']"));
ClickAdmin.click();
WebElement ClickPIM = driver.findElement(By.xpath("//*[text()='PIM']"));
ClickPIM.click();
}
}
This is my testng.xml file
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="Suite">
<test name="After Login Test">
<classes>
<class name="afterLogIn.NextLogIn"/>
</classes>
</test> <!-- Test -->
</suite> <!-- Suite -->
While running the program I get the Exception
[RemoteTestNG] detected TestNG version 7.4.0
driver is null
FAILED: DashboardPage
java.lang.NullPointerException
at afterLogIn.NextLogIn.DashboardPage(NextLogIn.java:22)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
The Webdriver url from the parent class is not passing to the child class. Is their any Issues with my code. When I run the parent class with main method it navigate the browser to the url but when I call from the child class it not giving the same url.
the class member driver is not instantiated, in the method logInCreated() in the parent class you are using another object reference ( not the class member ).
in the LoginPage add a no arg constructor in wich you instntialize driver
public LoginPage(){ driver = new ChromeDriver(); }
and in the method LogInCredit() delete the line WebDriver driver= new ChromeDriver();
enter image description here //Base class
package com.IVAPP.qa.Base;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
public class BaseClass {
public static Properties prop;
public static WebDriver driver;
public BaseClass() throws IOException{
try{
prop = new Properties();
//prop.load(this.getClass().getResourceAsStream("C:\\Users\\Jomonli\\workspace\\IVAPP_Automation\\src\\main\\java\\com\\IVAPP\\qa\\Config\\Config.properties"));
FileInputStream ip = new FileInputStream(System.getProperty("C:\\Users\\Jomonli\\workspace\\IVAPP_Automation\\src\\main\\java\\com\\IVAPP\\qa\\Config\\Config.properties"));
prop.load(ip);
}catch(FileNotFoundException e){
e.printStackTrace();
}catch(IOException e){
e.printStackTrace();
}
}
public void intialization(){
// String browserName = prop.getProperty("browser");
//
// if(browserName.equals("IE")){
// System.setProperty("webdriver.ie.driver","C:\\Users\\Jomonli\\Desktop\\IEDriverServer_x64_3.14.0\\IEDriverServer.exe");
// //driver = new InternetExplorerDriver(); }
File file = new File("C:\\Users\\Jomonli\\Desktop\\IEDriverServer_x64_3.14.0\\IEDriverServer.exe");
System.setProperty("webdriver.ie.driver", file.getAbsolutePath());
driver = new InternetExplorerDriver();
driver.manage().window().maximize();
driver.get(prop.getProperty("url"));
}
}
This is the base class which is extended to class which I'm trying to run.
LoginPageTest
package com.IVAPP.qa.OffShoreTestCases;
import java.io.IOException;
import org.openqa.selenium.WebDriver;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import com.IVAPP.qa.Base.BaseClass;
import com.IVAPP.qa.OffShorePage.HomePage;
import com.IVAPP.qa.OffShorePage.LoginPage;
public class LoginPageTest extends BaseClass {
LoginPage loginpage;
HomePage homePage;
WebDriver driver;
public LoginPageTest() throws IOException{
super();
}
#BeforeMethod
public void setUp() throws IOException{
intialization();
// String exePath = "C:\\Users\\Jomonli\\Desktop\\IEDriverServer_x64_3.14.0\\IEDriverServer.exe";
// //For use of IE only; Please enable for IE Browser
// System.setProperty("webdriver.ie.driver", exePath);
// driver = new InternetExplorerDriver();
this.loginpage =new LoginPage();
homePage = new HomePage();
}
#Test(description="Logging in with valid credentials")
public void LoginTest() throws IOException{
homePage = loginpage.login(prop.getProperty("UserID"), prop.getProperty("Password"));
}
#AfterMethod
public void teardown(){
driver.quit();
}
}
=================================================================================================
Testng.xml File
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite">
<test name="Test">
<classes>
<class name="com.IVAPP.qa.OffShoreTestCases.LoginPageTest"/>
</classes>
</test> <!-- Test -->
</suite> <!-- Suite -->
This is testng .xml and it points to right class which I'm trying to run, but it says it cannot instantiate the class. Does this have something to do with driver path.
I tried Project>Clean>Update maven,Changed the path,wrote different code but Nothing worked
Exception errors
org.testng.TestNGException:
Cannot instantiate class com.IVAPP.qa.OffShoreTestCases.LoginPageTest
at org.testng.internal.ObjectFactoryImpl.newInstance(ObjectFactoryImpl.java:40)
at org.testng.internal.ClassHelper.createInstance1(ClassHelper.java:373)
at org.testng.internal.ClassHelper.createInstance(ClassHelper.java:285)
at org.testng.internal.ClassImpl.getDefaultInstance(ClassImpl.java:126)
at org.testng.internal.ClassImpl.getInstances(ClassImpl.java:191)
at org.testng.TestClass.getInstances(TestClass.java:104)
at org.testng.TestClass.initTestClassesAndInstances(TestClass.java:90)
at org.testng.TestClass.init(TestClass.java:82)
at org.testng.TestClass.<init>(TestClass.java:45)
at org.testng.TestRunner.initMethods(TestRunner.java:422)
at org.testng.TestRunner.init(TestRunner.java:252)
at org.testng.TestRunner.init(TestRunner.java:222)
at org.testng.TestRunner.<init>(TestRunner.java:171)
at org.testng.remote.support.RemoteTestNG6_9_10$1.newTestRunner(RemoteTestNG6_9_10.java:28)
at org.testng.remote.support.RemoteTestNG6_9_10$DelegatingTestRunnerFactory.newTestRunner(RemoteTestNG6_9_10.java:61)
at org.testng.SuiteRunner$ProxyTestRunnerFactory.newTestRunner(SuiteRunner.java:604)
at org.testng.SuiteRunner.init(SuiteRunner.java:170)
at org.testng.SuiteRunner.<init>(SuiteRunner.java:117)
at org.testng.TestNG.createSuiteRunner(TestNG.java:1359)
at org.testng.TestNG.createSuiteRunners(TestNG.java:1346)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1200)
at org.testng.TestNG.runSuites(TestNG.java:1124)
at org.testng.TestNG.run(TestNG.java:1096)
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)
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at org.testng.internal.ObjectFactoryImpl.newInstance(ObjectFactoryImpl.java:29)
... 25 more
Caused by: java.lang.NullPointerException
at java.util.Properties$LineReader.readLine(Unknown Source)
at java.util.Properties.load0(Unknown Source)
at java.util.Properties.load(Unknown Source)
at com.IVAPP.qa.Base.BaseClass.<init>(BaseClass.java:28)
at com.IVAPP.qa.OffShoreTestCases.LoginPageTest.<init>(LoginPageTest.java:23)
... 30 more
These are the two exception i m getting
As you see the exception is appearing while trying to read the config file.
Caused by: java.lang.NullPointerException
at java.util.Properties$LineReader.readLine(Unknown Source)
at java.util.Properties.load0(Unknown Source)
at java.util.Properties.load(Unknown Source)
could you please make changes to the config path?
FileInputStream ip = new FileInputStream("Config/Config.properties");
prop.load(ip);
I have a problem with my code. I want to test some elements on a website but after run the tests, TestNG throws the error: "No tests were found".
I have already tried to create a new testng.xml but it doesn't work.
package tests;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;
import org.testng.Assert;
import org.testng.annotations.*;
import pages.LoginToAutomationAccount;
public class TestsOnLogin {
WebDriver driver;
// #FindBy(css = "a[class='login'][rel='nofollow']")
// WebElement signInButton;
//
public TestsOnLogin(WebDriver driver){
this.driver=driver;
//PageFactory.initElements(driver, this);
}
LoginToAutomationAccount account;
#BeforeSuite
public void setUp() {
System.setProperty("webdriver.chrome.driver", "C:\\Users\\Gabriel\\Downloads\\chromedriver.exe");
driver = new ChromeDriver();
driver.get("http://automationpractice.com/index.php");
}
#Test
public void test_HomePageLogin() {
//Locate Sign-In button and press it to start the adventure
driver.findElement(By.cssSelector("a[class='login'][rel='nofollow']")).click();
//completion for object -account-
account = new LoginToAutomationAccount(driver);
//login to website account
account.loginAutomationPage("gabriel.noki9#gmail.com", "capptain3");
//verify if it is logged in
String textConfirmation = account.getConfirmationLogin();
Assert.assertTrue(textConfirmation.contains("My account"));
}
#AfterSuite
public void downPage(){
driver.quit();
}
}
TestNG file - I tried to modify the link but it doesn't work
<?xml version="1.0" encoding ="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="LoginToAutomationAccount">
<test name="testngTest">
<packages>
<package name="tests" />
</packages>
</test>
</suite>
My Testcases package has the below code
package Testcases;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.Test;
import Objectrepository.FBloginpage;
public class Testcase1 {
#Test
public void login() {
System.setProperty("webdriver.chrome.driver", "C:\\Work\\chromedriver.exe");
WebDriver driver=new ChromeDriver();
driver.get("https://www.facebook.com");
FBloginpage fb= new FBloginpage(driver);
fb.Email().sendKeys("sample#gmail.com");
fb.Password().sendKeys("Password");
fb.Login().click();
}
}
My Objectrepository package has the below code in FBloginpage.java
package Objectrepository;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
public class FBloginpage {
WebDriver driver;
By username = By.id("email");
By password = By.name("pass");
By login = By.xpath("//input[#type='submit']");
public FBloginpage (WebDriver driver) {
this.driver= driver;
}
public WebElement Email() {
return driver.findElement(username);
}
public WebElement Password() {
return driver.findElement(password);
}
public WebElement Login() {
return driver.findElement(login);
}
}
My Testng.xml file has the below code
<?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">
<classes>
<class name="Testcases.Testcase1"/>
</classes>
</test> <!-- Test -->
</suite> <!-- Suite -->
When i execute the Testcases1.java file i am getting the below error
[RemoteTestNG] detected TestNG version 6.14.2
FAILED: login
java.lang.NoClassDefFoundError: com/google/common/collect/ImmutableMap
at org.openqa.selenium.remote.service.DriverService$Builder.<init>(DriverService.java:249)
at org.openqa.selenium.chrome.ChromeDriverService$Builder.<init>(ChromeDriverService.java:96)
at org.openqa.selenium.chrome.ChromeDriverService.createDefaultService(ChromeDriverService.java:89)
at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:123)
at Tescases.Testcase1.login(Testcase1.java:14)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.base/java.lang.reflect.Method.invoke(Unknown Source)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:124)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1137)
at org.testng.TestNG.runSuites(TestNG.java:1049)
at org.testng.TestNG.run(TestNG.java:1017)
at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:114)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)
Caused by: java.lang.ClassNotFoundException: com.google.common.collect.ImmutableMap
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(Unknown Source)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(Unknown Source)
at java.base/java.lang.ClassLoader.loadClass(Unknown Source)
... 30 more
Tried using updated chromedriver but still the problem persist.I am using Eclipse IDE and i imported all the necessary libraries
I too have same issue when I started working. The solution I opted was to first delete all the referenced Selenium files and then download new ones and then assigned them to a folder and added their reference and then it will start working properly.
The code I am using is shown below.
MultiBrowser
package com;
import java.io.File;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
public class MultiBrowser {
public WebDriver driver;
// Passing Browser parameter from TestNG xml
#Parameters("browser")
#BeforeClass
public void beforeTest(String browser) {
// If the browser is Firefox, then do this
if(browser.equalsIgnoreCase("firefox")) {
driver = new FirefoxDriver();
}else if (browser.equalsIgnoreCase("ie")) {
DesiredCapabilities capabilities = DesiredCapabilities.internetExplorer();
capabilities.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, true);
File fil = new File("C:\\IEDriver\\IEDriverServer.exe");
System.setProperty("webdriver.ie.driver", fil.getAbsolutePath());
driver = new InternetExplorerDriver(capabilities);
}
driver.get("http://www.store.demoqa.com");
}
#Test
public void login() throws InterruptedException{
driver.findElement(By.xpath(".//*[#id='account']/a")).click();
driver.findElement(By.id("log")).sendKeys("testuser_1");
driver.findElement(By.id("pwd")).sendKeys("Test#123");
driver.findElement(By.id("login")).click();
}
#AfterClass
public void afterTest(){
driver.quit();
}
}
TestNG.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite" parallel="none">
<test name="FirefoxTest">
<parameter name="browser" value="firefox" />
<classes>
<class name="com.MultiBrowser" />
</classes>
</test>
<test name="IETest">
<parameter name="browser" value="ie" />
<classes>
<class name="com.MultiBrowser" />
</classes>
</test>
</suite>
It works fine when I use Firefox but I get the below issue when I run the same code on IE
Exception :
Unable to find element with xpath == .//*[#id='account']/a (WARNING: The server did not provide any stacktrace information)
It needs to set Security level in all zones. To do that follow the steps below:
1.Open IE
2.Go to Tools -> Internet Options -> Security
3.All check boxes in Security should be enabled.