How can I initialize Chrome Remote web driver using WebDriverManager while passing ChromeOptions and a Selenium Grid standalone server url? - selenium

Trying to initialize a Chrome Remote web driver using WebDriverManager, while passing ChromeOptions and a Selenium Grid standalone server URL using Java.
From online examples;
Passing Chrome options would look like this:
WebDriverManager.chromedriver().setup();
RemoteWebDriver remoteWebDriver = new ChromeDriver(options);
threadLocalDriver.set(remoteWebDriver);
Passing the hub URL for the selenium grid standalone server would look like this:
WebDriverManager.chromedriver().setup();
RemoteWebDriver remoteWebDriver = ((RemoteWebDriver) WebDriverManager
.chromedriver()
.remoteAddress(hubURL)
.create());
threadLocalDriver.set(remoteWebDriver);
How can I pass both to a RemoteWebDriver object?
Thanks
EDIT:
Here is my code. I am getting an error from create() method
[main] ERROR io.github.bonigarcia.wdm.WebDriverManager - There was an error creating WebDriver object for Chrome
io.github.bonigarcia.wdm.config.WebDriverManagerException: Timeout of 30 seconds creating WebDriver object
public void createDriver() throws IOException {
ChromeOptions options = getPlatformSpecificOptions();
logger.info("Driver options: " + options.toString());
String hubURL = "http://127.0.0.1:4444/wd/hub";
WebDriver driver = WebDriverManager.chromedriver()
.capabilities(options)
.remoteAddress(hubURL)
.create();
threadLocalDriver.set(((RemoteWebDriver) driver));
}
TestHelper.setPlatform(PLATFORM);
}

You need to use the method capabilities() for that:
ChromeOptions options = new ChromeOptions();
options.addArguments("--headless");
WebDriver driver = WebDriverManager.chromedriver()
.capabilities(options)
.remoteAddress(hubURL)
.create();
Also calling setup() is not needed as it is called by create() method.

Related

Selenium doesn't call web page in Chrome

I'm using Chrome browser version 57.0.2987.110 and I'm trying to open up a web page with Selenium, only thing is it's only opening the browser with the standard website but is not opening the web page I told it to. It's not an option to change the browser, I have to work with chrome.
I get this error right here:
Request for unknown Channel-associated interface: ui::mojom::GpuMain
I tried this solution so far, but it didn't work for me: Strange error in selenium after upgrading to chromedriver 2.28 needed for chrome 57
This is my code:
public static void main(String[] args) {
WebDriver driver;
String PROXY;
Proxy proxy;
proxy = new Proxy();
PROXY = "proxy.myproxy:8080";
ChromeOptions options = new ChromeOptions();
options.addArguments("--disable-gpu");
System.setProperty("webdriver.chrome.driver", "/usr/bin/google-chrome");
proxy.setHttpProxy(PROXY).setFtpProxy(PROXY).setSslProxy(PROXY);
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(CapabilityType.PROXY, proxy);
capabilities.setCapability(ChromeOptions.CAPABILITY, options);
driver = new ChromeDriver(capabilities);
driver.get("www.facebook.com");
}
I've just been starting with selenium and just wrote down everything really quick to try it out in main class. I know it's not pretty. Please bear with me. What am I doing wrong?
You need to update your both selenium jars and update your chrome browser as well

Exception in thread "main" java.lang.NullPointerException using WebDriver Driver=new Chrome() and maximize Chrome browser window using selenium script

How to maximize Chrome browser window using selenium script?
This is my code:
package newpackage;
import org.openqa.selenium.WebDriver;
public class MyClass {
public static void main(String[] args) {
WebDriver Driver=new Chrome();
Driver.get("http://www.google.com");
Driver.manage().window().maximize();
}
}
However, I get this error:
Exception in thread "main" java.lang.NullPointerException
at newpackage.MyClass.main(MyClass.java:10)
To work with Selenium 3.4.0 you need to download the latest chromedriver 2.29 from here and update your Google Chrome to latest release of 58.x. Save the chromedriver in your system and provide the absolute path in your code through System.setProperty as below.
Now, the constructor for initializing ChromeDriver and Chrome Browser is as follows:
WebDriver driver = new ChromeDriver();
WebDriver driver = new ChromeDriver(options);
Note: The method is ChromeDriver() but not Chrome() which have caused java.lang.NullPointerException
Finally, to maximize Chrome browser window using selenium script you need to take help of ChromeOptions class as follows:
public static void main(String[] args)
{
System.setProperty("webdriver.chrome.driver", "C:\\path\\to\\chromedriver.exe");
ChromeOptions options = new ChromeOptions();
options.addArguments("start-maximized");
WebDriver driver = new ChromeDriver(options);
driver.navigate().to("https://google.com");
//do your actions
driver.quit();
}
In the script you have written the Driver object is null. Try instantiate the Driver properly by using Chromedriver.
System.setProperty("webdriver.chrome.driver", "c:\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
Driver.get("http://www.google.com");
Driver.manage().window().maximize();
var options = new ChromeOptions();
options.AddArguments("disable-infobars");
options.AddArguments("--start-maximized");
options.AddArguments("--disable-extensions");
var chromeDriver = new ChromeDriver(options);
use driver.manage().window().fullscreen(); instead
It should work.

How to run selenium code

I have the code written for selenium, but I do not know how to make selenium read the code. I know that you have to put it in a certain folder somewhere though. I have never used this program or anything like it before. I just need to know how to make selenium read my code. I have no idea what I'm doing here.
public class GoogleTest {
WebDriver driver = new ChromeDriver();
String baseUrl = "google.com";;
Selenium selenium = new WebDriverBackedSelenium(driver, baseUrl);
selenium.open("google.com");
selenium.type("name=q", "cheese");
selenium.click("name=btnG");
WebDriver driverInstance = ((WebDriverBackedSelenium) selenium).getUnderlyingWebDriver();
}
You need to write the following line before creating the driver object:
System.setProperty("webdriver.chrome.driver", "C:/path/to/chrome/driver/chrome.exe");
Try following code in same order:
System.setProperty("webdriver.chrome.driver", "C:/Program Files (x86)/Google/Chrome/Application/chrome.exe");
WebDriver driver = new ChromeDriver();
String baseUrl = "google.com";;
Selenium selenium = new WebDriverBackedSelenium(driver, baseUrl);
.
.
You may need to download the chrome driver as well.
link: https://code.google.com/p/selenium/wiki/ChromeDriver

Using Selenium to test Safari gives GridException

I am using the code from this site (http://darrellgrainger.blogspot.com/2011/02/using-selenium-20-with-webdriver-and.html) to run Selenium tests in Safari 5. The code goes like this:
Selenium sel = new DefaultSelenium("localhost", 4444, "*safari", baseURL);
CommandExecutor executor = new SeleneseCommandExecutor(sel);
DesiredCapabilities dc = new DesiredCapabilities();
WebDriver browser = new RemoteWebDriver(executor, dc);
browser.get("http://www.google.com");
WebElement input = browser.findElement(By.name("q"));
input.sendKeys("Selenium");
So I start a Selenium server standalone version on the localhost machine and I register a test node (also on localhost) to the Selenium hub. Then I start the test. I then get the following exception: org.openqa.selenium.WebDriverException: Could not start Selenium session: org%2Eopenqa%2Egrid%2Ecommon%2Eexception%2EGridException%3A+Error+forwarding+the+new+session+The+server+returned+an+error+%3A+
I don't know what error. There is no server output on the console. Does anyone have ideas? I used the newest version (2.17.0) of Selenium.
EDIT: I just tried "firefox" instead of safari and it outputs the same exception. So actually it's not the fault of safari. Maybe there is something wrong with executing Selenium 1 code via the grid?
Try This:
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setBrowserName("safari");
capabilities.setJavascriptEnabled(true);
CommandExecutor executor = new SeleneseCommandExecutor(new URL("http://localhost:5555/"), new URL("http://www.google.com/"), capabilities);
WebDriver driver = new RemoteWebDriver(executor, capabilities);
driver.get("http://google.com");
Don't create DefaultSelenium object. The above code works well for me with Safari browser.

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");
}