I am trying to perform basic selenium operations using page object pattern. However, when I run the test, I see browser being opened along with given url, but fails the test by complaining it couldn't initialise the declared web element. Can some one please help me to understand what I'm missing here. Below are my base class, page object class and test class.
BasePage
public class Basepage {
public static WebDriver driver;
public Basepage(){}
public Basepage(WebDriver driver){
this.driver = driver;
PageFactory.initElements(driver,this);
}
public void OpenBrowser(){
System.setProperty("webdriver.chrome.driver",
"C:\\SeleniumWork\\chromedriver_win32\\chromedriver.exe");
driver = new ChromeDriver();
driver.navigate().to("https://www.google.com");
}
Page object class
public class SecurityMainPage extends Basepage{
public SecurityMainPage(WebDriver driver){
this.driver = driver;
PageFactory.initElements(driver,this);
}
#FindBy( xpath = "//a[text()='What We Do']")
WebElement dropdown;
public void selectOption(String option){
Actions actions = new Actions(driver);
actions.moveToElement(dropdown).click().build().perform(); //dropdown element is returning null
}
TestClass
public class SecurityMainPageTests extends Basepage {
protected static SecurityMainPage sec = new SecurityMainPage(driver);
public SecurityMainPageTests(){}
#BeforeTest
public void setup(){
OpenBrowser();
}
#Test
public void selectOptionTest(){
sec.selectOption("Networking");
}
#AfterTest
public void tearDown(){
closeBrowser();
}
Log
org.openqa.selenium.json.JsonException: java.lang.reflect.InvocationTargetException
Build info: version: '3.13.0', revision: '2f0d292', time: '2018-06-25T15:24:21.231Z'
System info: host: 'LAPTOP-5J2KPMM3', ip: '192.168.0.36', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '11.0.7'
Driver info: driver.version: RemoteWebDriver
at org.openqa.selenium.json.JsonOutput.convertUsingMethod(JsonOutput.java:302)
at org.openqa.selenium.json.JsonOutput.lambda$new$13(JsonOutput.java:139)
at org.openqa.selenium.json.JsonOutput$SafeBiConsumer.accept(JsonOutput.java:394)
at org.openqa.selenium.json.JsonOutput.write(JsonOutput.java:248)
at org.openqa.selenium.json.JsonOutput.lambda$null$18(JsonOutput.java:152)
at java.base/java.lang.Iterable.forEach(Iterable.java:75)
at org.openqa.selenium.json.JsonOutput.lambda$new$19(JsonOutput.java:152)
at org.openqa.selenium.json.JsonOutput$SafeBiConsumer.accept(JsonOutput.java:394)
at org.openqa.selenium.json.JsonOutput.write(JsonOutput.java:248)
at org.openqa.selenium.json.JsonOutput.lambda$null$20(JsonOutput.java:161)
at com.google.common.collect.SingletonImmutableBiMap.forEach(SingletonImmutableBiMap.java:65)
at org.openqa.selenium.json.JsonOutput.lambda$new$21(JsonOutput.java:160)
at org.openqa.selenium.json.JsonOutput$SafeBiConsumer.accept(JsonOutput.java:394)
at org.openqa.selenium.json.JsonOutput.write(JsonOutput.java:248)
at org.openqa.selenium.json.JsonOutput.write(JsonOutput.java:239)
at org.openqa.selenium.json.Json.toJson(Json.java:42)
at org.openqa.selenium.remote.http.AbstractHttpCommandCodec.encode(AbstractHttpCommandCodec.java:227)
at org.openqa.selenium.remote.http.AbstractHttpCommandCodec.encode(AbstractHttpCommandCodec.java:117)
at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:152)
at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:83)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:548)
at org.openqa.selenium.remote.RemoteWebDriver.perform(RemoteWebDriver.java:614)
at org.openqa.selenium.interactions.Actions$BuiltAction.perform(Actions.java:638)
at test.java.SecurityMainPage.selectOption(SecurityMainPage.java:37)
at test.java.SecurityMainPageTests.selectOptionTest(SecurityMainPageTests.java:28)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:85)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:639)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:816)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1124)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:108)
at org.testng.TestRunner.privateRun(TestRunner.java:774)
at org.testng.TestRunner.run(TestRunner.java:624)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:359)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:354)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:312)
at org.testng.SuiteRunner.run(SuiteRunner.java:261)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1191)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1116)
at org.testng.TestNG.run(TestNG.java:1024)
at com.intellij.rt.testng.IDEARemoteTestNG.run(IDEARemoteTestNG.java:66)
at com.intellij.rt.testng.RemoteTestNGStarter.main(RemoteTestNGStarter.java:110)
Suppressed: org.openqa.selenium.json.JsonException: Attempting to close incomplete json stream
Build info: version: '3.13.0', revision: '2f0d292', time: '2018-06-25T15:24:21.231Z'
Driver info: driver.version: RemoteWebDriver
at org.openqa.selenium.json.JsonOutput.close(JsonOutput.java:263)
at org.openqa.selenium.json.Json.toJson(Json.java:44)
... 32 more
Caused by: java.lang.reflect.InvocationTargetException
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at org.openqa.selenium.json.JsonOutput.convertUsingMethod(JsonOutput.java:298)
... 47 more
Caused by: java.lang.NullPointerException
at org.openqa.selenium.support.pagefactory.DefaultElementLocator.findElement(DefaultElementLocator.java:69)
at org.openqa.selenium.support.pagefactory.internal.LocatingElementHandler.invoke(LocatingElementHandler.java:38)
at com.sun.proxy.$Proxy9.getWrappedElement(Unknown Source)
at org.openqa.selenium.interactions.PointerInput$Origin.asArg(PointerInput.java:203)
at org.openqa.selenium.interactions.PointerInput$Move.encode(PointerInput.java:154)
at org.openqa.selenium.interactions.Sequence.encode(Sequence.java:75)
at org.openqa.selenium.interactions.Sequence.toJson(Sequence.java:84)
... 52 more
For easy understanding, I split the driver initiation and navigation to url to two methods in Basepage. And you haven't declared the closeBrowser() method.
Base Class
public class Basepage {
public static WebDriver driver;
public Basepage(){}
public Basepage(WebDriver driver){
this.driver = driver;
PageFactory.initElements(driver,this);
}
public WebDriver initiateDriver(){
System.setProperty("webdriver.chrome.driver",
"C:\\SeleniumWork\\chromedriver_win32\\chromedriver.exe");
driver = new ChromeDriver();
return driver;
}
public void openBrowser(String url){
driver.navigate().to(url);
}
}
Test Class
public class SecurityMainPageTests extends Basepage {
WebDriver driver = null;
protected static SecurityMainPage sec = null;
String url = "https://www.google.com";
public SecurityMainPageTests(){}
#BeforeTest
public void setup(){
driver = initiateDriver();
openBrowser(String url);
sec = new SecurityMainPage(driver);
}
#Test
public void selectOptionTest(){
sec.selectOption("Networking");
}
#AfterTest
public void tearDown(){
closeBrowser();
}
}
Related
I've used POM model with 2 pages . LoginPage & HomePage . these 2 pages has 2 test classes (LoginTest & HomeTest) along with BaseTest(Driver initialization purposes & being inherited by all 4 classes) . While clicking loginpage.verifyLogin(email_id , pswrd) , the homepage is supposed to be opened . I'm getting java.lang.ExceptionInInitializerError while executing login testcase.
PS. It is working fine if return type is changed to void for verifyLogin().
Refer the BaseTest.
public class BaseTest
{
public static WebDriver driver ;
public static String chrome_path ="/home/downloads/chromedriver";
public static String property_file_path ="/home/CompleteMotorJourneyAutoTest/src/test/java/com/qa/utils/loginDetails.properties";
public BaseTest()
{
try {
FileInputStream fis = new FileInputStream(property_file_path);
Properties prop = new Properties();
prop.load(fis);
}catch(Exception e) {System.out.println("Exception occured during reading the property file !");}
}
public static void initialization()
{ System.setProperty("webdriver.chrome.driver",chrome_path);
driver=new ChromeDriver();
driver.get(prop.getProperty("URL"));
}
}
LoginPage
public class LoginPage extends BaseTest
{
#FindBy(xpath="//*[#id=\"loginform\"]/div[1]/div[2]/input")
WebElement email_id_field;
#FindBy(xpath="//*[#id=\"loginform\"]/div[2]/div[2]/input")
WebElement password_field;
#FindBy(xpath="//*[#id='loginform']/input")
WebElement login_button_Field;
public LoginPage()
{ PageFactory.initElements(driver, this); }
public HomePage verifyLogin(String username,String password)
{ email_id_field.sendKeys(username);
password_field.sendKeys(password);
login_button_Field.click();
return new HomePage();
}
}
LoginTest
public class LoginTest extends BaseTest
{ LoginPage loginpage;
HomePage homepage;
public LoginTest()
{ super(); }
//Browser initialization/Environment setup <----------------------
#BeforeMethod
public void setup()
{ initialization();
loginpage = new LoginPage(); }
//login with correct credentials <----------------------
#Test(priority=0)
public void loginWithCorrectCredentials()
{ homepage=loginpage.verifyLogin(prop.getProperty("login_id"),prop.getProperty("login_password"));
}
//Browser closure <----------------------
#AfterMethod
public void tearDown()
{ driver.quit(); }
}
I need to test Homepage as well but stuck since getting
[RemoteTestNG] detected TestNG version 7.0.1
Starting ChromeDriver 86.0.4240.22 (398b0743353ff36fb1b82468f63a3a93b4e2e89e-refs/branch-heads/4240#{#378}) on port 8944
Only local connections are allowed.
Please see https://chromedriver.chromium.org/security-considerations for suggestions on keeping ChromeDriver safe.
ChromeDriver was started successfully.
Oct 28, 2020 3:55:51 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: W3C
FAILED CONFIGURATION: #BeforeMethod setup
java.lang.ExceptionInInitializerError
at com.qa.testpages.LoginPage.verifyLogin(LoginPage.java:115)
at com.qa.testcase.HomeTest.setup(HomeTest.java:35)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:134)
at org.testng.internal.MethodInvocationHelper.invokeMethodConsideringTimeout(MethodInvocationHelper.java:63)
at org.testng.internal.ConfigInvoker.invokeConfigurationMethod(ConfigInvoker.java:348)
at org.testng.internal.ConfigInvoker.invokeConfigurations(ConfigInvoker.java:302)
at org.testng.internal.TestInvoker.runConfigMethods(TestInvoker.java:695)
at org.testng.internal.TestInvoker.invokeMethod(TestInvoker.java:523)
at org.testng.internal.TestInvoker.invokeTestMethod(TestInvoker.java:173)
at org.testng.internal.MethodRunner.runInSequence(MethodRunner.java:46)
at org.testng.internal.TestInvoker$MethodInvocationAgent.invoke(TestInvoker.java:816)
at org.testng.internal.TestInvoker.invokeTestMethods(TestInvoker.java:146)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:146)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:128)
at java.base/java.util.ArrayList.forEach(ArrayList.java:1541)
at org.testng.TestRunner.privateRun(TestRunner.java:766)
at org.testng.TestRunner.run(TestRunner.java:587)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:384)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:378)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:337)
at org.testng.SuiteRunner.run(SuiteRunner.java:286)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:53)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:96)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1187)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1109)
at org.testng.TestNG.runSuites(TestNG.java:1039)
at org.testng.TestNG.run(TestNG.java:1007)
at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:115)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)
Caused by: java.lang.NullPointerException
at com.qa.testpages.HomePage.<clinit>(HomePage.java:32)
... 34 more
SKIPPED CONFIGURATION: #AfterMethod tearDown
Can anyone please help . Thanks in advance !
It's closed. Issue was with HomePage.java class which has the xpaths & methods for HomeTest.java class. The HomePage.java class had static WebElements which had incorrect xpaths. Now , corrected the xpaths & found it working fine .
I created a test class using TestNG and in this class I defined 5 test methods. When I run the test, the TestNG console shows that 3 scenarios of the total of 5 have been executed (see the image below).
The registroNovo and tabulacaoTratamento methods are not executed.
When I close the browser (Chrome) the TestNG console displays the following error screen that follows below.
Eclipse console displays the following error message:
org.openqa.selenium.WebDriverException: chrome not reachable
(Session info: chrome=67.0.3396.87)
(Driver info: chromedriver=2.40.565498 (ea082db3280dd6843ebfb08a625e3eb905c4f5ab),platform=Windows NT 6.1.7601 SP1 x86_64) (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 0 milliseconds
Build info: version: '3.8.0', revision: '924c4067df', time: '2017-11-30T11:36:59.109Z'
System info: host: 'RJO-BCC-C2A-H21', ip: '10.5.79.38', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.8.0_131'
Driver info: org.openqa.selenium.chrome.ChromeDriver
Capabilities {acceptInsecureCerts: false, acceptSslCerts: false, applicationCacheEnabled: false, browserConnectionEnabled: false, browserName: chrome, chrome: {chromedriverVersion: 2.40.565498 (ea082db3280dd6..., userDataDir: C:\Users\lsnpere\AppData\Lo...}, cssSelectorsEnabled: true, databaseEnabled: false, handlesAlerts: true, hasTouchScreen: false, javascriptEnabled: true, locationContextEnabled: true, mobileEmulationEnabled: false, nativeEvents: true, networkConnectionEnabled: false, pageLoadStrategy: normal, platform: XP, platformName: XP, rotatable: false, setWindowRect: true, takesHeapSnapshot: true, takesScreenshot: true, unexpectedAlertBehaviour: , unhandledPromptBehavior: , version: 67.0.3396.87, webStorageEnabled: true}
Session ID: 32d0291ab5ecaa1141f9997771edcd95
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:214)
at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:166)
at org.openqa.selenium.remote.http.JsonHttpResponseCodec.reconstructValue(JsonHttpResponseCodec.java:40)
at org.openqa.selenium.remote.http.AbstractHttpResponseCodec.decode(AbstractHttpResponseCodec.java:80)
at org.openqa.selenium.remote.http.AbstractHttpResponseCodec.decode(AbstractHttpResponseCodec.java:44)
at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:164)
at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:83)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:601)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:658)
at org.openqa.selenium.remote.RemoteWebDriver$RemoteTargetLocator.alert(RemoteWebDriver.java:987)
at org.openqa.selenium.support.ui.ExpectedConditions$28.apply(ExpectedConditions.java:808)
at org.openqa.selenium.support.ui.ExpectedConditions$28.apply(ExpectedConditions.java:804)
at org.openqa.selenium.support.ui.FluentWait.until(FluentWait.java:208)
at page.RegistroNovo.registroNovo(RegistroNovo.java:25)
at test.ValidarFaturamentoRoamingTest.registroNovo(ValidarFaturamentoRoamingTest.java:34)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:85)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:639)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:816)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1124)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:108)
at org.testng.TestRunner.privateRun(TestRunner.java:774)
at org.testng.TestRunner.run(TestRunner.java:624)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:359)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:354)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:312)
at org.testng.SuiteRunner.run(SuiteRunner.java:261)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1215)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1140)
at org.testng.TestNG.run(TestNG.java:1048)
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)
Has anyone ever seen this kind of error?
Basically I created 4 classes:
The BaseTest class which has all methods that are common to all test classes:
public class BaseTest {
public static WebDriver driver;
public static WebDriverWait wait;
#BeforeClass
public static void setup() {
System.setProperty("webdriver.chrome.driver", "e:\\Selenium\\chromedriver.exe");
driver = new ChromeDriver();
wait = new WebDriverWait(driver, 15);
driver.manage().window().maximize();
}
#AfterClass
public static void tearDown() throws InterruptedException {
Thread.sleep(5000);
driver.findElement(By.id("mn_sair"));
driver.quit();
}}
The test class ValidarFaturamentoRoamingTest which extends the class BaseTest:
public class ValidarFaturamentoRoamingTest extends BaseTest {
#Test(priority=0)
public void acessarBkoMais() {
HomePage homepage = new HomePage(driver, wait);
homepage.abrirBkoMais();
}
#Test(priority=1)
public void logarBkoMais() {
LoginPage logar = new LoginPage(driver, wait);
logar.loginBkoMais("000000", "000000");
}
#Test(priority=2)
public void logarEstrategia() {
LogarEstrategiaPage logarEstrategia = new LogarEstrategiaPage(driver, wait);
logarEstrategia.logarEstrategia();
}
#Test(priority=3)
public void registroNovo() {
RegistroNovo registroNovo = new RegistroNovo(driver, wait);
registroNovo.registroNovo();
}
#Test(priority=4)
public void tabulacaoTratamento() {
TratOsFatRoamPage rt = new TratOsFatRoamPage(driver, wait);
rt.TratOsFatRoam();
}}
The class TratOsFatRoamPage which extends the class BasePage.
public class TratOsFatRoamPage extends BasePage {
public TratOsFatRoamPage(WebDriver driver, WebDriverWait wait) {
super(driver, wait);
}
String idMotivo = "cboMotivo";
String idSubMotivo = "cboSubMotivo";
String idStatus = "cboStatus";
String qtdNumerosFaturas = "txtQtdFaturas";
String qtdNumerosCotas = "txtQtdContas";
String idObservacao = "txtObservacao";
String idSalvar = "btnSalvar";
public void TratOsFatRoam() {
writeText(By.id(qtdNumerosFaturas),"Teste 2018");
writeText(By.id(qtdNumerosCotas), "2018-2");
}}
And the class BasePage which has all the methods that are common classes:
public class BasePage {
public WebDriver driver;
public WebDriverWait wait;
public BasePage(WebDriver driver, WebDriverWait wait) {
this.driver = driver;
this.wait = wait;
}
public void click(By elementLocation) {
driver.findElement(elementLocation).click();
}
public void writeText(By elementLocation, String text) {
driver.findElement(elementLocation).sendKeys(text);
}
public String readText(By elementLocation) {
return driver.findElement(elementLocation).getText();
}
public void selectText(By elementLocation, String value) {
Select atividade = new Select(driver.findElement(elementLocation));
atividade.selectByValue(value);
}}
This error message...
org.openqa.selenium.WebDriverException: chrome not reachable
...implies that the ChromeDriver was unable to communicate with the WebBrowser i.e. Chrome Browsing session.
Your main issue is the incompatibility between the version of the binaries you are using as follows:
You are using chromedriver=2.40
You are using chrome=67.0
Your Selenium Client version is 3.8.0 of 2017-11-30T11:36:59.109Z which is almost six months older.
Your JDK version is 1.8.0_131 which is pretty ancient.
So there is a clear mismatch between the JDK v8u131 , Selenium Client v3.8.0 , ChromeDriver v2.40 and Chrome Browser v67.0
org.openqa.selenium.WebDriverException: chrome not reachable
This means that for some reason there is no communication happening between driver and chrome instance; in your case that reason is you are closing it manually after 3 Tests are run.
and your TestNG console displays the error screen because last 2 test cases weren't run due to closing of browser.
So here your real issue is what is taking second last test to take time?
For that post some code.
I am unable to find what is the cause of the error "org.openqa.selenium.WebDriverException" in my code, Please help me find and understand the cause much better. Firstly I would like to explain my code, It is a simple code where I go to a website click on "Sign In" link and then click on a text "Email" text field for registration purpose. I have used Page Factory in this code.
I have 3 files.
- Base file (Before and After methods)
- Registration File (Object repository and methods)
- Registration_Testcase File (Calling the methods)
Note: Base and Registration Testcase classes are in one package and Registration is in another package.
**Below is my code:**
Base Class
package testcases;
import org.testng.annotations.BeforeTest;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.openqa.selenium.firefox.internal.ProfilesIni;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.AfterTest;
public class Base {
public WebDriver driver;
public WebDriverWait wait;
#BeforeTest
public void beforeTest() {
ProfilesIni pro = new ProfilesIni();
FirefoxProfile ffProfile = pro.getProfile("vishvesh");
ffProfile.setAcceptUntrustedCertificates(true);
ffProfile.setAssumeUntrustedCertificateIssuer(false);
String base_url = "http://automationpractice.com/index.php";
System.setProperty("webdriver.gecko.driver", "G:/Workplace/AutomationSetupFiles/Geckdriver/geckodriver.exe");
driver = new FirefoxDriver();
driver.get(base_url);
}
#AfterTest
public void afterTest() {
driver.close();
}
}
Registration Class
package pages;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;
import org.openqa.selenium.support.ui.ExpectedConditions;
import testcases.Base;
public class Registration extends Base{
#FindBy(xpath="//*[#id='header']/div[2]//div[1]/a")
WebElement SignIn;
#FindBy(xpath="//input[#id='email_create']")
WebElement Registration_Email;
public Registration(WebDriver driver){
this.driver=driver;
PageFactory.initElements(driver, this);
}
public void ClickSignIn(){
//wait.until(ExpectedConditions.visibilityOf(SignIn)).click();
SignIn.click();
}
public void EnterEmail(String y){
//wait.until(ExpectedConditions.visibilityOf(Registration_Email)).click();
Registration_Email.click();
Registration_Email.sendKeys(y);
}
}
Registration TestCase Class
package testcases;
import org.testng.annotations.Test;
import pages.Registration;
public class Registatration_testcase extends Base{
#Test
public void f() {
Registration regobj = new Registration(driver);
regobj.ClickSignIn();
regobj.EnterEmail("amdv1991#gmail.com");
}
}
Error What I get:
FAILED CONFIGURATION:
#BeforeTest beforeTest
org.openqa.selenium.WebDriverException:
Build info: version: 'unknown', revision: '8c03df6', time: '2017-03-02 09:32:39 -0800'
System info: host: 'LAPTOP-SCJ4OHK5', ip: '192.168.1.3', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '1.8.0_131'
Driver info: org.openqa.selenium.firefox.FirefoxDriver
Capabilities [{moz:profile=C:\Users\VISHVE~1\AppData\Local\Temp\rust_mozprofile.XaA5YbSD8jeh, rotatable=false, timeouts={implicit=0, pageLoad=300000, script=30000}, pageLoadStrategy=normal, platform=ANY, specificationLevel=0, moz:accessibilityChecks=false, acceptInsecureCerts=false, browserVersion=53.0.3, platformVersion=10.0, moz:processID=7440, browserName=firefox, platformName=windows_nt}]
Session ID: 1ad0b6c1-bb5c-4306-afa6-b4e3dcb62ac1
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.openqa.selenium.remote.http.W3CHttpResponseCodec.createException(W3CHttpResponseCodec.java:127)
at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:93)
at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:42)
at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:163)
at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:82)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:604)
at org.openqa.selenium.remote.RemoteWebDriver.get(RemoteWebDriver.java:325)
at testcases.Base.beforeTest(Base.java:30)
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:108)
at org.testng.internal.Invoker.invokeConfigurationMethod(Invoker.java:523)
at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:224)
at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:146)
at org.testng.TestRunner.beforeRun(TestRunner.java:626)
at org.testng.TestRunner.run(TestRunner.java:594)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:380)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:375)
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:1301)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1226)
at org.testng.TestNG.runSuites(TestNG.java:1144)
at org.testng.TestNG.run(TestNG.java:1115)
at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:132)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:230)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:76)
I have seemed to have resolved this issue, Looked like there was some compatibility issues. Now I am using the following configurations
selenium 3.4
Firefox browser v48
geckodriver 17
I am very new to appium:
i wanted to run a code where in my device i open chrome and open a google.com:
#BeforeMethod
public void setUp() throws Exception {
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("browserName", "Browser");
capabilities.setCapability("device", "Android");
capabilities.setCapability("deviceName", "TA9330416L");
capabilities.setCapability("platformVersion", "5.1");
capabilities.setCapability("platformName", "Android");
capabilities.setCapability("appPackage", "com.android.chrome");
driver = new RemoteWebDriver(new URL("http://127.0.0.1:4723/wd/hub"),capabilities);
}
#AfterMethod
public void tearDown() throws Exception {
driver.quit();
}
#Test
public void launchWebsite()throws InterruptedException {
driver.get("http://www.google.com");
}
but getting the following error:
?* FAILED CONFIGURATION: #BeforeMethod setUp
java.lang.NoClassDefFoundError: com/google/common/base/Function
at WhatsApp.setUp(WhatsApp.java:36)
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:86)
at org.testng.internal.Invoker.invokeConfigurationMethod(Invoker.java:514)
at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:215)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:589)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:820)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1128)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:129)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:112)
at org.testng.TestRunner.privateRun(TestRunner.java:782)
at org.testng.TestRunner.run(TestRunner.java:632)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:366)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:361)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:319)
at org.testng.SuiteRunner.run(SuiteRunner.java:268)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1246)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1171)
at org.testng.TestNG.run(TestNG.java:1066)
at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:113)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:206)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:177)
Caused by: java.lang.ClassNotFoundException: com.google.common.base.Function
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 27 more
SKIPPED CONFIGURATION: #AfterMethod tearDown
SKIPPED: launchWebsite*/
You need to add this dependendcy jar https://code.google.com/p/guava-libraries/
Have to created any test class? Error says no class definition found. You have to create on test class first and define functions in the class. I have not tried on the android browser but I have automated android hybrid and native apps using appium.
package test;
import java.net.URL;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import io.appium.java_client.android.AndroidDriver;
public class TestBrowser {
public AndroidDriver driver;
public TestBrowser() {
//To do
}
#BeforeMethod
public void setUp() throws Exception {
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("appium-version", "1.0");
capabilities.setCapability("browserName", "Browser");
capabilities.setCapability("platformName", "Android");
capabilities.setCapability("deviceName", "TA9330416L");
capabilities.setCapability("platformVersion", "5.1");
capabilities.setCapability("appPackage", "com.android.chrome");
driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
}
#AfterMethod
public void tearDown() throws Exception {
if(driver!=null)
driver.quit();
}
#Test
public void launchWebsite()throws InterruptedException {
driver.get("http://www.google.com");
}
}
I am getting this error:
org.openqa.selenium.remote.UnreachableBrowserException: Error communicating with the remote browser. It may have died.
Build info: version: '2.45.0', revision: '32a636c', time: '2015-03-05 22:01:35'
System info: host: 'HOME', ip: '10.1.11.121', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.8.0_31'
Driver info: driver.version: RemoteWebDriver
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:593)
at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:352)
at org.openqa.selenium.remote.RemoteWebDriver.findElementByName(RemoteWebDriver.java:425)
at org.openqa.selenium.By$ByName.findElement(By.java:299)
at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:344)
at com.tests.TestClass2.test2(TestClass2.java:14)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:84)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:714)
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)
Caused by: org.apache.http.conn.HttpHostConnectException: Connect to 127.0.0.1:7055 [/127.0.0.1] failed: Connection refused: connect
at org.apache.http.impl.conn.HttpClientConnectionOperator.connect(HttpClientConnectionOperator.java:140)
at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.connect(PoolingHttpClientConnectionManager.java:318)
at org.apache.http.impl.execchain.MainClientExec.establishRoute(MainClientExec.java:363)
at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:219)
at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:195)
at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:86)
at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:108)
at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:184)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:72)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:57)
at org.openqa.selenium.remote.internal.ApacheHttpClient.fallBackExecute(ApacheHttpClient.java:126)
at org.openqa.selenium.remote.internal.ApacheHttpClient.execute(ApacheHttpClient.java:72)
at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:133)
at org.openqa.selenium.firefox.internal.NewProfileExtensionConnection.execute(NewProfileExtensionConnection.java:165)
at org.openqa.selenium.firefox.FirefoxDriver$LazyCommandExecutor.execute(FirefoxDriver.java:362)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:572)
... 29 more
Caused by: java.net.ConnectException: Connection refused: connect
at java.net.DualStackPlainSocketImpl.waitForConnect(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:85)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:345)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:589)
at org.apache.http.conn.socket.PlainConnectionSocketFactory.connectSocket(PlainConnectionSocketFactory.java:72)
at org.apache.http.impl.conn.HttpClientConnectionOperator.connect(HttpClientConnectionOperator.java:123)
... 44 more
My Environment:
OS: Windows 7
JDK: javac 1.8.0_31
FireFox: ESR 31.4.0
Eclipse: 4.4.2.20150219-0708
Selenium: selenium-2.45.0
Testng: 6.8.6.20141201_2240
I am using a Testing suite file to run 2 classes (TestClass1 & TestClass 2). It throws above error when invoking browser for 2nd class. Here is my complete Project
Base Class:
public class BaseClass {
public static WebDriver driver = new FirefoxDriver();
public static boolean implicitwait(long time) {
try {
driver.manage().timeouts().implicitlyWait(time, TimeUnit.SECONDS);
} catch (Exception e) {
return false;
}
return true;
}
#BeforeClass
public static boolean LaunchBrowser() throws InterruptedException {
implicitwait(50000);
try {
System.setProperty("webdriver.firefox.bin",
"C:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe");
driver.navigate().to("https:google.com");
driver.manage().window().maximize();
implicitwait(5000);
} catch (Exception e) {
return false;
}
System.out.println("Launching Browser & Navigating To Page");
return true;
}
#AfterClass
public void CloseBrowser() {
driver.close();
System.out.println("Closing Browser");
}
}
Test Class:
#Test
public class TestClass extends BaseClass {
public void test1(){
System.out.println("TEST 1");
driver.findElement(By.name("q")).sendKeys("ABC");
driver.findElement(By.name("btnG")).click();
}
}
Test Class2
#Test
public class TestClass2 extends BaseClass {
public void test2(){
System.out.println("TEST 2");
driver.findElement(By.name("q")).sendKeys("DEF");
driver.findElement(By.name("btnG")).click();
}
}
I would super appreciate if someone can let me know what I am doing wrong
My Testng.xml:
<test name="Run Test Class In Firefox">
<classes>
<class name="com.tests.TestClass" />
<class name="com.tests.TestClass2" />
</classes>
</test>
Use #BeforeSuite and #AfterSuite instead of #BeforeClass and #AfterClass
#AfterClass
public void CloseBrowser() {
driver.close();
System.out.println("Closing Browser");
}
Basically closing the current browser instance after the first class and thus, the driver instance is not valid to run the tests for next class. You can test this by commenting out the #AfterClass
Reference
EDIT
Video
Gist