Selenium :UnreachableBrowserException is shown in chromeDriver with testNG - selenium-webdriver

When I run the following code, below error is showing : org.openqa.selenium.remote.UnreachableBrowserException: Could not start a new session. Possible causes are invalid address of the remote server or browser start-up failure.
The chrome browser is not getting launched.
//baseClass.java:
public class BaseClass {
//ThreadLocal will keep local copy of driver
public static ThreadLocal<RemoteWebDriver> dr = new ThreadLocal<RemoteWebDriver>();
#BeforeTest
//Parameter will get browser from testng.xml on which browser test to run
#Parameters("myBrowser")
public void beforeClass(String myBrowser) throws MalformedURLException{
try {
RemoteWebDriver driver = null;
if(myBrowser.equals("chrome")){
DesiredCapabilities capability = new DesiredCapabilities().chrome();
capability.setBrowserName("chrome");
capability.setPlatform(Platform.WINDOWS);
driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), capability);
}
else if(myBrowser.equals("firefox")){
DesiredCapabilities capability = new DesiredCapabilities().firefox();
capability.setBrowserName("firefox");
capability.setPlatform(Platform.WINDOWS);
driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), capability);
}
//setting webdriver
setWebDriver(driver);
getDriver().manage().window().maximize();
getDriver().manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
}catch (Exception ex){
System.out.println(ex.toString());
}
}
public WebDriver getDriver() {
return dr.get();
}
public void setWebDriver(RemoteWebDriver driver) {
dr.set(driver);
}
#AfterClass
public void afterClass(){
getDriver().quit();
dr.set(null);
}
}

You have to set the system property for chrome/gecko driver before initializing the RemoteWebDriver. Something like,
if(myBrowser.equals("chrome")){
DesiredCapabilities capability = new DesiredCapabilities().chrome();
capability.setBrowserName("chrome");
capability.setPlatform(Platform.WINDOWS);
System.setProperty("webdriver.chrome.driver", "C:\\chromedriver\\chromedriver.exe");
driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), capability);
}
else if(myBrowser.equals("firefox")){
DesiredCapabilities capability = new DesiredCapabilities().firefox();
capability.setBrowserName("firefox");
capability.setPlatform(Platform.WINDOWS);
System.setProperty("webdriver.gecko.driver", "C:\\geckodriver\\geckodriver.exe");
driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), capability);
}

Related

Using Java, how can I implement an UI cucumber scenario that gives entry data to the Webdriver while maintaing ability to run locally and remotely?

I have a .properties file that gives input data for instantiating the WebDriver inside the ServiceHooks class.
How can I change this logic to use a cucumber .feature file to drive the parameters that I provide to the WebDriver and implement such a scenario?
Feature: Login page navigation
Scenario Outline: User navigates to url
Given I open the browser <browser> on operatingSystem <platform>
When I navigate to url <url>
Then I see the login page
Examples:
|browser|platform| url |
|"firefox"|"windows"|"https://loginpage.com"|
|"chrome"| "windows" |"https://loginpage.com"|
I have some code that evaluates if the driver is remote or not and instantiates the driver (thus opens the browser already) before the test scenario gets executed.
public class ServiceHooks {
public static WebDriver driver;
public void getDriverInstance() {
//if isRemoteDriver evaluests to true or false base on a .properties file
if (isRemoteDriver.equals("yes")){
driver = getRemoteDriverInstance(platform, browser, url);
} else {
driver = getLocalDriverInstance(platform, browser, url);
}
//platform, browser, url are served from the .properties file as well
public WebDriver getLocalDriverInstance(String platform, String browser, String url)
throws MalformedURLException {
WebDriver localDriver = null;
DesiredCapabilities capabilities = new DesiredCapabilities();
//ChromeOptions options = new ChromeOptions();
// Platforms
if (platform.equalsIgnoreCase("Windows")) {
capabilities.setPlatform(Platform.WINDOWS);
}
if (platform.equalsIgnoreCase("MAC")) {
capabilities.setPlatform(Platform.MAC);
}
// Browsers
if (browser.equalsIgnoreCase("chrome")) {
capabilities = DesiredCapabilities.chrome();
localDriver = new ChromeDriver();
}
if (browser.equalsIgnoreCase("firefox")) {
capabilities = DesiredCapabilities.firefox();
localDriver = new FirefoxDriver();
}
if (browser.equalsIgnoreCase("ie")) {
capabilities = DesiredCapabilities.internetExplorer();
localDriver = new InternetExplorerDriver();
}
localDriver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
localDriver.manage().window().maximize();
localDriver.get(url);
return localDriver;
}
public static WebDriver getRemoteDriverInstance(String platform, String browser, String url)
throws MalformedURLException {
String nodeURL = "http://localhost:4444/wd/hub";
WebDriver remoteDriver = null;
DesiredCapabilities capabilities = new DesiredCapabilities();
// Platforms
if (platform.equalsIgnoreCase("Windows")) {
capabilities.setPlatform(Platform.WINDOWS);
}
if (platform.equalsIgnoreCase("MAC")) {
capabilities.setPlatform(Platform.MAC);
}
// Browsers
if (browser.equalsIgnoreCase("chrome")) {
capabilities = DesiredCapabilities.chrome();
}
if (browser.equalsIgnoreCase("firefox")) {
capabilities = DesiredCapabilities.firefox();
}
if (browser.equalsIgnoreCase("ie")) {
capabilities = DesiredCapabilities.internetExplorer();
}
// Open the Application
remoteDriver = new RemoteWebDriver(new URL(nodeURL), capabilities);
remoteDriver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
remoteDriver.manage().window().maximize();
remoteDriver.get(url);
return remoteDriver;
}
}
I want to be able to run the mentioned cucumber test scenario locally using WebDriver an well as remotely with Selenium Grid.

How could I maximize chrome browser's window using driver.manage().window() .maximize(); in a script for cross browser testing

I have written a script for Cross Browser Testing in Selenium Webdriver using TestNG Parameters.But In this script,driver.manage().window()
.maximize(); is not working from chrome.How Could I maximize all browsers' window?
public class WithTestNG {
WebDriver driver;
#Parameters("browser")
#BeforeClass
{
if(browser.equalsIgnoreCase("firefox"))
System.setProperty("webdriver.gecko.driver","/Users/Preet/Desktop
/Path/geckodriver" );
driver = new FirefoxDriver();
}
else if (browser.equalsIgnoreCase("ie"))
{
System.setProperty("webdriver.ie.driver","C:\\Users\\CP\\Downloads\\IED
riverServer_x64_3.4.0\\IEDriverServer.exe");
driver = new InternetExplorerDriver();
}
else if (browser.equalsIgnoreCase("chrome"))
{
System.setProperty("webdriver.chrome.driver","/Users/Preet12/Desktop/Pa
th/chromedriver" );
driver = new ChromeDriver();
}
driver.manage().window().maximize();
}
#Test(priority=0)
public void OpenStore()
{
String URL = "www.facebook.com";
driver.get(URL);
String Actual_URL = driver.getCurrentUrl();
String Expected_URL = "https://www.facebook.com/";
Assert.assertEquals(Actual_URL, Expected_URL, "URL doesn't match");
System.out.println("URL verified");
}
Replace:
driver.manage().window().maximize();
with:
driver.manage().window().fullscreen();

Selenium opens chrome browser not the web page

I'm trying to open a website in three browser using selenium grid. when i'm running my script,firefox and ie working fine.But chrome browser is opening with "data:," not with a URL.
Selenium Version 2.47
Chromedriver.exe 2.20
Chrome 46.0
Can any one tell me why?
#Parameters("browser")
#BeforeTest
public void launchapp(String browser) throws MalformedURLException
{
String URL = "http://www.tutorialspoint.com/selenium/selenium_grids.htm";
if (browser.equalsIgnoreCase("firefox"))
{
System.out.println(" Executing on FireFox");
String Node = "http://10.101.7.220:5555/wd/hub";
DesiredCapabilities cap = DesiredCapabilities.firefox();
cap.setBrowserName("firefox");
driver = new RemoteWebDriver(new URL(Node), cap);
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
// Launch website
driver.navigate().to(URL);
driver.manage().window().maximize();
}
else if (browser.equalsIgnoreCase("ie"))
{
System.out.println(" Executing on IE");
System.setProperty("webdriver.ie.driver","IEDriverServer.exe");
DesiredCapabilities capabilities = DesiredCapabilities
.internetExplorer();
capabilities
.setCapability(
InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS,
true);
capabilities.setCapability("ignoreZoomSetting", true);
capabilities.setCapability("nativeEvents", false);
//driver = new RemoteWebDriver(new URL(Node), capabilities);
driver= new InternetExplorerDriver(capabilities);
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
// Launch website
driver.navigate().to(URL);
driver.manage().window().maximize();
}
else if (browser.equalsIgnoreCase("chrome"))
{
System.out.println("Running Chrome");
System.setProperty("webdriver.chrome.driver", "C:\\Users\\mob150003576\\Downloads\\chromedriver.exe");
driver = new ChromeDriver();
}
else
{
throw new IllegalArgumentException("The Browser Type is Undefined");
}
}
#Test
public void sample()
{
driver.findElement(By.xpath("//a[text()=' Home']")).click();
}`
One thing I notice in your code is that you do not use RemoteWebDriver for running your IE or Chrome tests. The code can be simplified to read:
String URL = "http://www.tutorialspoint.com/selenium/selenium_grids.htm";
System.setProperty("webdriver.chrome.driver", "chromedriver.exe");
System.setProperty("webdriver.ie.driver", "IEDriverServer.exe");
DesiredCapabilities caps = null;
switch(browser){
case "chrome" : caps = DesiredCapabilities.chrome();
break;
case "firefox" : caps = DesiredCapabilities.firefox();
break;
case "ie" : caps = DesiredCapabilities.internetExplorer();
caps.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS,true);
break;
}
driver = new RemoteWebDriver(new URL("grid string"), caps);
driver.navigate().to(URL);
driver.manage().window().maximize();
Please update the paths to browser driver as per your PC and add the specific capabilities you need.

Nullpointer Exception in webdriver, driver is null

I am a newbie in selenium java and trying to run test case by calling methods from another class in a framework. Methods are defined in a different class. The issue is that for first instance, the driver is found (Firefox driver) but when a method from another class is called, the driver is null.
Below is the code snippet:
public class ActionDriver {
WebDriver driver;
public ActionDriver(WebDriver driver){
this.driver = driver;
}
public void type(By loc, String value) {
driver.findElement(loc).sendKeys(value);
}
}
NPE comes for the above line of code for driver for second instance when method is called from the WebAction class. driver.findElement(loc).sendKeys(value);
Second class is :
public class WebActions {
WebDriver driver;
ActionDriver ad = new ActionDriver(driver);
SizeChartTemplate sct = new SizeChartTemplate();
public void TypeTemplateName(){
ad.type(jsct.Template_Name, "Men's Vest");
}
}
NPE is for the above line of code at ad.
Test Case:
public class LoginToJcilory extends OpenAndCloseBrowser {
#Test
public void logintojcilory() throws BiffException, IOException, InterruptedException{
WebActions wa = new WebActions();
System.out.println("Entered login to jcilory block");
ActionDriver actdrvr = new ActionDriver(driver);
JciloryLoginPage jclp = new JciloryLoginPage();
JcilorySizeChartTemplate jsct = new JcilorySizeChartTemplate();
String username = actdrvr.readExcel(0, 1);
String password = actdrvr.readExcel(1, 1);
System.out.println(username);
System.out.println(password);
actdrvr.type(jclp.Username, username);
actdrvr.type(jclp.Password, password);
actdrvr.click(jclp.LoginButton);
Thread.sleep(2000);
driver.get("http://qacilory.dewsolutions.in/JCilory/createSizeChartTemplate.jc");
wa.TypeTemplateName();
}
NPE comes for wa element in the above code.
Below is the error:
FAILED: logintojcilory
java.lang.NullPointerException
at Config.ActionDriver.type(ActionDriver.java:40)
at Config.WebActions.TypeTemplateName(WebActions.java:17)
at Test.LoginToJcilory.logintojcilory(LoginToJcilory.java:55)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
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.access$000(SuiteRunner.java:37)
at org.testng.SuiteRunner$SuiteWorker.run(SuiteRunner.java:368)
at org.testng.internal.thread.ThreadUtil$2.call(ThreadUtil.java:64)
at java.util.concurrent.FutureTask.run(FutureTask.java:262)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:745)
===============================================
Test run on FIREFOX
Tests run: 1, Failures: 1, Skips: 0
===============================================
OpenAndCloseBrowser:
public class OpenAndCloseBrowser {
protected WebDriver driver;
#Parameters({"browser","baseURL"})
#BeforeClass
public void openBrowser(String browser,String baseURL){
if(browser.equalsIgnoreCase("firefox")){
driver=new FirefoxDriver();
}
else if(browser.equalsIgnoreCase("chrome")){
System.setProperty("webdriver.chrome.driver", System.getProperty("user.dir")+"\\driver\\chromedriver.exe");
driver=new ChromeDriver();
}
else if(browser.equalsIgnoreCase("ie")){
System.setProperty("webdriver.ie.driver", System.getProperty("user.dir")+"\\driver\\IEDriverServer.exe");
DesiredCapabilities caps=new DesiredCapabilities();
caps.setCapability(InternetExplorerDriver.IGNORE_ZOOM_SETTING, true);
caps.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, true);
driver=new InternetExplorerDriver(caps);
}
else{
driver=new FirefoxDriver();
}
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(30,TimeUnit.SECONDS);
driver.get(baseURL);
}
#AfterClass
public void closeBrowser(){
//driver.quit();
}
}
I guess there is an issue with the way i am defining the driver in these classes. Any help is resolving the issue is appreciated.
Your driver is null in ActionDriver.type() because it was null when it was passed into the constructor from WebActions. OpenAndCloseBrowser is creating a WebDriver instance, but assigning to a DIFFERENT (local) driver variable. You should learn more about variable scoping in Java...this isn't a Selenium/Webdriver issue.
You probably don't need an answer anymore, but this issue has quite the views, so just for completeness.
I usually have a global driver variable in my test class and I instantiate it like this, so it's available for every test.
#Before
public void setUp() throws Exception {
System.setProperty("webdriver.gecko.driver", "path/to/geckodriver");
DesiredCapabilities capabilities = DesiredCapabilities.firefox();
capabilities.setCapability("marionette", true);
driver = new FirefoxDriver(capabilities);
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
}
Just add this to your test class (adapt driver path) and the driver is instantiated before the test.

Getting driver value as null in selenium web driver for appium mobile automation testing

private WebDriver driver;
#BeforeMethod
public void setUp() throws Exception {
// set up appium
BasicConfigurator.configure();
File appDir = new File("This PC\\GT-I9100\\Phone\\360");
File app = new File(appDir, "app-release.apk"); //my case “demo1.apk”
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("device","Android");
capabilities.setCapability(CapabilityType.BROWSER_NAME, "");
capabilities.setCapability(CapabilityType.VERSION, "4.2");
capabilities.setCapability(CapabilityType.PLATFORM, "WINDOW");
capabilities.setCapability("app", app.getAbsolutePath());
capabilities.setCapability("app-package", "app-release.apk"); //my case com.gorillalogic.monkeytalk.demo1
capabilities.setCapability("app-activity", "Login"); //my case RootActivity
driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
}
#AfterMethod
public void tearDown() throws Exception {
driver.quit();
}
#Test
public void loginTest() throws Throwable
{
System.out.println("Hello");
System.out.println(driver);
setUp();
}
#Test
public void formTest() throws InterruptedException
{
System.out.println("Hello");
System.out.println(driver);
/*Getting driver value as null in selenium web driver for appium mobile automation testing
Getting driver value as null.
Used device name then also i am getting null value
I have connected my real device.*/
Add a try/catch block when instantiating the AndroidDriver() ...
Maybe there is something wrong there. Try this code
new DesiredCapabilities();
DesiredCapabilities capabilities = DesiredCapabilities.android();
capabilities.setCapability(MobileCapabilityType.PLATFORM_NAME,"Android");
capabilities.setCapability(MobileCapabilityType.BROWSER_NAME,"Chrome");
capabilities.setCapability(MobileCapabilityType.DEVICE_NAME,"0123456789ABCDEF");
capabilities.setCapability(MobileCapabilityType.PLATFORM_VERSION,"4.4");
try
{
linker = new URL("http://127.0.0.1:4723/wd/hub");
driver = new AndroidDriver(linker, capabilities);
driver.manage().timeouts().pageLoadTimeout(120, TimeUnit.SECONDS);
}
catch (MalformedURLException e)
{
System.out.println("URL init error");
}
Cheers

Resources