Skip to main content

Posts

Showing posts with the label webdriver

Can't wait to wait -- Webdriver Waits!!

Wait!!! These four letters can drive you crazy if you are having a bad day using Webdriver... Thus if you are reading this article you are already in the middle of one bad bad day...thus I'll cut the crap and go directly into the ways you can add waits using webdriver: Type 1 - Using driver.manager.timeouts() Implicit Waits - When you set this wait, webdriver would wait for this amount of time before failing any command. Syntax -  driver . manage (). timeouts (). implicitlyWait ( 10 , TimeUnit . SECONDS ); Example - Webdriver is looking to find an element using an xpath and its not able to find it, webdriver waits for 10 seconds before trying to find the element and thus failing if it still can't find the element after 10 seconds Page Load Timeouts - When you set this wait, webdriver would wait for this amount of time to declare page load timeouts Syntax -  driver . manage (). timeouts (). pageLoadTimeout ( 100 , SECONDS ); Example - Let's say we ar...

Selenium Exception: java.lang.RuntimeException: Could not start Selenium session

You have a simple Java Selenium Code. All of it looks perfect :) except for when you try running it, it fails miserably again and again with an exception:  java . lang . RuntimeException : Could not start Selenium session The possible solutions for the above issue can be the following: You repeated the "selenium.start()" issue - Looks like if we create a webdriver first and then create a selenium object thereafter, an internal issue arises with the selenium server where it fails when we use selenium.start() So if you are wondering on how to solve - Use selenium.open() instead

Issues Running Selenium Scripts Remotely

In real time we hardly have a simple setup like running on the same box which acts as a server and client :( So we might have to run the scripts remotely almost all the time...Well there might be issues while doing so...Here are some! Can't launch the browser at all: Check the host name you are using in your code first! There is a possibility that the DNS isn't able to map the hostname with the IP - so try running your tests specifying the IP instead of the hostname Check the path you provided to the executable of the browser - verify its found on the same path on your remote location Now the final check is to verify you can run the browser version on the RC version for ex: you can't run FF7 on RC1 :)  Browser Opens but tests aren't running This happens because the RC isn't able to talk to your browser - though it initiates the browser but can't talk to it anymore Solution: Upgrade your RC :)  Running on a Headless Box Selenium can't run u...

Difference between isVisible() and isElementPresent()

Many a times I wondered what's the difference between the two functions. Here is what I have: isElementPresent() - This method basically tests if the element we are looking for is present somewhere on the page. isVisible() - looks for display: none style tag - this might throw a null pointer if we aren't careful...thus to see if an element is visible first check if the element is present using isElementPresent() method. Then try checking if the element is visible! Observe that isElementPresent() won't mind even if our element is not visible. For ex: lets say the below is the html code for a component on my test application: now if you test the above component with selenium.isElementPresent("testinput") - returns true! selenium.isVisible("testinput") - returns false!

Running Firefox with Firebug

Many a times our XPATH's would fail on test execution. This is when we would want to run the browser with the firebug extension. Just add this section of code in your code:  File file = new File("firebug-1.5.4.xpi"); FirefoxProfile firefoxProfile = new FirefoxProfile(); firefoxProfile.addExtension(file); WebDriver driver = new FirefoxDriver(firefoxProfile); 

Test Enabling/Disabling JavaScript on a browser

import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebDriverBackedSelenium; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.firefox.FirefoxProfile; import com.thoughtworks.selenium.Selenium; public class TestJS { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub FirefoxProfile profile = new FirefoxProfile(); profile.setPreference("javascript.enabled", false); WebDriver driver = new FirefoxDriver(profile); Selenium selenium = new WebDriverBackedSelenium(driver, "http://www.somedomain.com"); selenium.open("/dp/gotopage/"); driver.findElement(By.id("prodImage")).click(); boolean result = driver.findElement(By.className("imageWarningMessage")).isEnabled(); if (result) System.out.println("Result is: "+driver....

Remote Web Driver Program

Just a simple class depeciting the usage of a RemoteWebDriver which will be useful for any beginner... import java.net.MalformedURLException; import java.net.URL; import org.openqa.selenium.WebDriver; import org.openqa.selenium.remote.DesiredCapabilities; import org.openqa.selenium.remote.RemoteWebDriver; public class TestRemote { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub DesiredCapabilities capability = DesiredCapabilities.firefox(); try { WebDriver driver = new RemoteWebDriver(new URL("http://computername:4444/wd/hub"), capability); driver.get("http://www.google.com"); } catch (MalformedURLException e) { } } } This will create a RemoteWebDriver which will run on a hub using firefox.