yandex

selenium webdriver tips & tricks

Sunday, 01 February 2015 07:14

There is no method verifyTextPresent in Selenium WebDriver, so if you need to check text on the page, you can use the following methods:

public boolean verifyTextPresent(String text) {
    return driver.getPageSource().contains(text);
}

or

public boolean verifyTextPresent(String text) {
    return driver.findElement(By.tagName("body")).getText().contains(text);
}

Working with frames

To work with frames, you can use the following methods.

I recommend the following method:

WebElement myFrame = driver.findElement(By.name("iframe_canvas"));
driver.switchTo().frame(myFrame);

You can switch to a frame by name:

driver.switchTo().frame("frame") 

As well as by index:

driver.switchTo().frame(0); 

Working with windows

To work with windows, I recommend using the following method:

public void switchToWindow(int numberWindow) {
    String handle = driver.getWindowHandles().toArray()[numberWindow].toString();
    driver.switchTo().window(handle);
}

Use the implicit waiting

I recommend always include implicit waiting, where they are not needed, just turn them off.

public void turnOnImplicitlyWait(int timeout) {
    driver.manage().timeouts().implicitlyWait(timeout, TimeUnit.SECONDS);
}

public void turnOffImplicitlyWait () {
    driver.manage().timeouts().implicitlyWait(0, TimeUnit.SECONDS);
}

Using Drag and Drop

Drag and Drop is performed in that way:

Actions builder = new Actions(driver);
Action dragAndDrop = builder.clickAndHold(someElement)
        .moveToElement(otherElement)
        .release(otherElement)
        .build();
dragAndDrop.perform();

Files uploading

If you have a file upload field <input type=file>, then implement a file download in the following way:

fileInput.sendKeys(file);

IntexSoft is in the Clutch Top 10 of Belarusian Web & Software Developers More