Skip to main content

Posts

Showing posts with the label selenium2

AJAX Testing–Some Useful Links

Testing dynamic elements is always a challenge. I found some interesting/useful links which might prove useful to you too: http://agilesoftwaretesting.com/selenium-wait-for-ajax-the-right-way/ http://stackoverflow.com/questions/6850994/wait-for-javascript-file-to-load-in-selenium-2 http://saucelabs.com/blog/index.php/2011/04/how-to-lose-races-and-win-at-selenium/   Happy Testing!!!!

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

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!

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.

Selenium2 - Read a Table

If I have a table which is designed in this manner: If you have an ID for the table its really good to catch the table else you will have to do alot of manipulation with the tables you find... // Here I try to identify the table using its ID. WebElement table = webdriver.findElement(By.id("tableID")); // Here I try to identify the table using a simple xpath WebElement table = webdriver.findElement(By.xpath("/html/body/table/tbody")); // Get all the rows of the table List rows = table.findElements(By.tagName("tr")); Iterator i = rows.iterator(); System.out.println("-----------------------------------------------"); // Iterate over the rows of the table while(i.hasNext()) { WebElement row = i.next(); // Find all the columns of the row List columns = row.findElements(By.tagName("td")); Iterator j = columns.iterator(); System.out.print(" | "); // Iterate over the columns of the ...