WebDriver not opening URL - selenium

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

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.

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

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.

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();

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

Selenium Chromedriver causes Chrome to start without configured plugins, bookmarks and other settings

I am a new user of Selenium. I want to use it to start up the Chrome browser but I have a problem.
public static void processor(String url, String name) {
System.setProperty("webdriver.chrome.driver", "C:/Documents and Settings/jingxiong/Local Settings/Application Data/Google/Chrome/Application/chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get(url);
WebElement element = driver.findElement(By.name(name));
element.sendKeys("google");
element.submit();
System.out.println("Page title is: " + driver.getTitle());
driver.quit();
}
When I run this example the Chrome browser starts ok but without configured plugins, my settings or bookmarks. What should I do to cause it load these?
Thank you.
You should first read chromedriver document in selenium wiki. Its available here - http://code.google.com/p/selenium/wiki/ChromeDriver
As mentioned in the wiki:-
Similarly, to load an extension when Chrome starts:
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
capabilities.setCapability("chrome.switches", Arrays.asList("--load-extension=/path/to/extension/directory"));
WebDriver driver = new ChromeDriver(capabilities);