How to get instance of RemoteWebDriver Selenium Grid? - selenium-grid

public RemoteWebDriver driver;
public void Login() throws Exception {
if (driver instanceof ChromeDriver || driver instanceof FirefoxDriver) {
driver.get(URL);
} else if (driver instanceof InternetExplorerDriver) {
driver.get(URL2);
enterCred();
} else if (driver instanceof OperaDriver) {
driver.get(URL2);
}
}
I am trying to get the instance of the RemoteWebDriver but the code above doesn't seem to work. I have to get a 2 separate URLs because of how internet explorer handles the login procedure. The code above worked when I was using just a normal webdriver but now that it is a RemoteWebDriver, IE is not being able to get the proper URL.
It will work if I take out everything from login to driver.get(URL); but only for Chrome and Firefox.

think i figured it out. i get do driver.toString(); and it will get something like this. [RemoteWebDriver: firefox on WINDOWS (6101278d-fc76-4459-9545-cf0e0052e30b)].
After i got that i just looked for the keyword "firefox", "chrome", or "internet explorer"

Related

Selenium JUnit tests not running org.openqa.selenium.NoSuchSessionException: Session ID is null. Using WebDriver after calling quit()?

I have created a java/selenium/cucumber automation framework with 4 feature files each testing different functional area's.
I can run each of these within Eclipse as feature files and they all run fine.
However I am trying to figure out how I can run them all as a JUnit test so that I can generate the extent reports.
I run the mainRunner class as a Junit test like ..
The first feature runs fine but then the next three do not. A chrome browser is opened for each of these but
the site is not navigated to. The error
'org.openqa.selenium.NoSuchSessionException: Session ID is null. Using WebDriver after calling quit()?'
is shown in the console logs.
So is this issue to do with how I have set my feature files up to use the startBrowser, initBrowser and closeBrowser methods. Which are
defined in a seperate class 'BrowserInitTearDownStepDefinitions'?
All my feature files look like ..
Feature: Update a Computer in Database
-- left out details
#StartBrowser
Scenario: Start browser session
When I initialize driver
Then open browser
#MyTest4
Scenario Outline: Update a computer
-- left our details
Examples:
-- left out details
#CloseBrowser
Scenario: Close the browser
Then close browser
I have had a look at the other posts related to this message that reference the same error, but can't see a clear cut solution.
As the error would indicate I seem to be quitting the driver but then not reinitialising it for the next test. Although Im not sure why this
would be the case given that each feature file has the #StartBrowser and #CloseBrowser tags which should execute these methods. Below is my BrowserInitTearDownStepDefinitions class.
public class BrowserInitTearDownStepDefinitions {
//Initialises Chrome browser
#When("^I initialize driver$")
public void initializeDriver() {
System.out.println("initializeDriver runs");
System.setProperty("webdriver.chrome.driver","C:\\xxxx\\CucumberAutomationFramework\\src\\test\\java\\cucumberAutomationFramework\\resources\\chromedriver.exe");
WebDriverUtils.setDriver(new ChromeDriver());
}
//Opens Chrome browser and clicks in search input box
#Then("^open browser$")
public void openBrowser() {
System.out.println("Open Browser runs");
WebDriver driver = WebDriverUtils.getDriver();
driver.manage().window().maximize();
driver.manage().timeouts().pageLoadTimeout(60, TimeUnit.SECONDS);
driver.manage().timeouts().setScriptTimeout(60, TimeUnit.SECONDS);
driver.get("https://computer-database.gatling.io/computers");
}
//Closes browser after all scenarios run and deletes cookies
#Then("^close browser$")
public void closeBrowser() {
System.out.println("Close Browser runs");
WebDriver driver = WebDriverUtils.getDriver();
driver.manage().deleteAllCookies();
driver.quit();
driver =null;
}
}
I create a new instance of the webdriver within my stepDefs file with ..
WebDriver driver = WebDriverUtils.getDriver()
The webdriver utils class code is ..
package cucumberAutomationFramework.utilityClasses;
import org.openqa.selenium.WebDriver;
public class WebDriverUtils {
private static WebDriver driver;
public static void setDriver(WebDriver webDdriver) {
if (driver == null) {
driver = webDdriver;
}
}
public static WebDriver getDriver() {
if (driver == null) {
throw new AssertionError("Driver is null. Initialize driver before calling this method.");
}
return driver;
}
}
Any advice would be much appreciated. Thanks.

org.testng.TestNGException: Cannot instantiate class EDRTermsPackge.ContactInformationTesting

I"m trying to resolve this issue but to no avail. Here is my Source Code
public class ContactInformationTesting {
//Using or Launching Internet Explorer
String exePath = "\\Users\\jj85274\\Desktop\\IEDriverServer.exe";
//For use of IE only; Please enable for IE Browser
WebDriver driver = new InternetExplorerDriver();
#Test
public void OpenPage_Login() {
driver.get("http://cp-qa.harriscomputer.com/");
}
You need to set the System property webdriver.ie.driver before instantiating the InternetExplorerDriver object. I could not find that in your code:
Try following:
System.setProperty("webdriver.ie.driver", "<path_to_your_IEDriverServer.exe>");
WebDriver driver = new InternetExplorerDriver();
Let me know, if you still get the same error.
UPDATE: I have assumed that IEDriverServer.exe's local path is C:\Users\jj85274\Desktop\IEDriverServer.exe. You can change it, as per its location on your machine. Try following code and let me know, whether you are able to launch Internet Explorer successfully.
System.setProperty("webdriver.ie.driver", "C:\\Users\\jj85274\\Desktop\\IEDriverServer.exe");
WebDriver driver = new InternetExplorerDriver();

Script runs fine in debug mode but not runs in run mode in IE Browser

i have tried following code both in Firefox and IE.
Firefox it is working fine but in IE it is not working . Im using IEv11 ,selenium v2.45 and also i tried with v2.46 and 2.44 .IE Driver Server v2.46. Please help me
public class IEBrowser {
WebDriver driver;
#Test
public void url(){
System.setProperty("webdriver.ie.driver", "E:\\IEDriverServer.exe");
driver = new InternetExplorerDriver();
driver.get("http://www.toolsqa.com/automation-practice-form/");
driver.findElement(By.name("firstname")).sendKeys("hi");
}
}
Use this code before finding the elements:
try {
Thread.sleep(10000);
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
This is applicable to any appium project. *Works in Android Appium too.
It's working fine in my System for all browsers.This may be due to internet speed as well.Any how add a WebDriverWait and see
driver.get("http://www.toolsqa.com/automation-practice-form/");
WebDriverWait wait = new WebDriverWait(driver, 40);//Unit in Seconds
wait.until(ExpectedConditions.visibilityOfElementLocated(By.name("firstname"))).sendKeys("hi");

Selenium - How to get a local WebDriver by Browser name

A RemoteWebDriver could be instantiated via browsername like "firefox" via DesiredCapabilties object.
But how to get a local driver like "FireFoxDriver" with browser name?
My use case is as follows: the browsers to be tested are specified via properties in an external text file. with another property the grid url is set. if grid url is set to "local" i want to run the tests local on the browsers set in text file.
(how) is this possible?
Sure. Something in the way of this?
public static void main(String[] args) {
WebDriver driver = openBrowser(args[0]);
// now work with driver as usual
}
public static WebDriver openBrowser(String browserName) {
if ((browserName == null) || (browserName.trim().isEmpty())) {
throw new IllegalArgumentException("No browser name found.");
}
// works with Java 7, on Java < 7, you have to write an if-else block instead
switch (browserName.toLowerCase()) {
case "ff": // fall through
case "firefox":
return new FirefoxDriver();
case "ie": // fall through
case "iexplore": // fall through
case "internet explorer":
return new InternetExplorerDriver();
default:
throw new IllegalArgumentException("No valid browser name found.");
}
}
I can suggest a hack. Launch selenium-server on your local machine and then you can use remotewebdriver with host url as http://localhost:4444/wd/hub.
This way you would be able to use same remote driver and external text file to control your tests on the local machine.

WebDriver not opening URL

I'm very new to selenium so I'm having trouble spotting the problem with my code. I'm using a webDriver backed selenium object, it starts the driver but never opens the URL and the driver just closes after a few moments. The last time this happened to me it was just because I had left "http" out of the URL. So what's causing it this time?
public void testImages() throws Exception {
Selenium selenium = new WebDriverBackedSelenium(driver, "http://www.testsite.com/login");
System.out.println(selenium.getXpathCount("//img"));
}
The setup looks like:
public void setUp() throws Exception {
System.setProperty("webdriver.chrome.driver", "C:\\Users\\User1\\Desktop\\chromedriver_win_16.0.902.0\\chromedriver.exe");
driver = new ChromeDriver();
Thread.sleep(2000);
}
The teardown method just consists of driver.close().
I'm using selenium 2.14 and the testNG Eclipse plug-in.
You might need to do the following
selenium.open("www.testsite.com/login");
Check out this example from the selenium site:
// You may use any WebDriver implementation. Firefox is used here as an example
WebDriver driver = new FirefoxDriver();
// A "base url", used by selenium to resolve relative URLs
String baseUrl = "http://www.google.com";
// Create the Selenium implementation
Selenium selenium = new WebDriverBackedSelenium(driver, baseUrl);
// Perform actions with selenium
selenium.open("http://www.google.com");
selenium.type("name=q", "cheese");
selenium.click("name=btnG");
// Get the underlying WebDriver implementation back. This will refer to the
// same WebDriver instance as the "driver" variable above.
WebDriver driverInstance = ((WebDriverBackedSelenium) selenium).getUnderlyingWebDriver();
//Finally, close the browser. Call stop on the WebDriverBackedSelenium instance
//instead of calling driver.quit(). Otherwise, the JVM will continue running after
//the browser has been closed.
selenium.stop();
link to selenium
You would need to add driver.get(url) like below.
public void setUp() throws Exception {
System.setProperty("webdriver.chrome.driver", "C:\\Users\\User1\\Desktop\\chromedriver_win_16.0.902.0\\chromedriver.exe");
driver = new ChromeDriver();
driver.get("http://www.testsite.com/login");
}