Q1. Handling Frames using selenium 2.0 (webdriver)
A1. You can use “switchTo” frame method to bring control on an HTML frame –
driver.switchTo().frame("frameName");
You can also use index number to specify a frame –
driver.switchTo().frame("parentFrame.4.frameName");
This would bring control on frame named – “frameName” of the 4th sub frame names “parentFrame”
Q2. How to navigate back and forth in a browser in Selenium 2.0?
A2. You can use Navigate interface to go back and forth in a page. Navigate method of WebDriver interface returns instance of Navigation. Navigate interface has methods to move back, forward as well as to refresh a page –
driver.navigate().forward();
driver.navigate().back();
driver.navigate().refresh();
Q3. How can I change User-Agent while using FF browser? I want to execute my tests with a specific User-Agent setting.
A3. You can create FF profile and add additional Preferences to it. Then this profile could be passed to Firefox driver while creating instance of Firefox –
FirefoxProfile profile = new FirefoxProfile();
profile.addAdditionalPreference("general.useragent.override", "User Agent String");
WebDriver driver = new FirefoxDriver(profile);
Q4. Is there any difference in XPath implementation in different WebDriver implementations?
A4. Since not all browsers (like IE) have support for native XPath, WebDriver provides its own implementation for XPath for such browsers. In case of HTMLUnitDriver and IEDriver, html tags and attributes names are considered lower cased while in case of FF driver they are considered case in-sensitive.
Q5. My application uses ajax highly and my tests are suffering from time outs while using Selenium 2.0L.
A5. You can state WebDriver to implicitly wait for presence of Element if they are not available instantly. By default this setting is set to 0. Once set, this value stays till the life span of WebDriver object. Following example would wait for 60 seconds before throwing ElementNotFound exception –
WebDriver driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
WebElement element = driver.findElement(By.id("elementID"));
Q6.What if I don’t want to use implicit wait and want to wait only for presence of certain elements?
A6. You can use explicit wait in this situation to wait for presence of certain element before continuing with test execution. You can use “WebDriverWait” and “ExpectedCondition” to achieve this –
WebDriver driver = new FirefoxDriver();
WebElement myDynamicElement = (new WebDriverWait(driver, 60)).until(new ExpectedCondition(){
@Override
public WebElement apply(WebDriver d) {
return d.findElement(By.id("myDynamicElement"));
}});
This is going to wait up to 60 seconds before throwing ElementNotFound exception.