Monday, 15 February 2016

Switching to New Tab/Window

Create Utils and Pass driver instance and windowTitle.

public static void moveToNewWindows(WebDriver driver, String windowTitle) {
    boolean windowExists = false;
    Set<String> windows = driver.getWindowHandles();
    for (String window : windows) {
        driver.switchTo().window(window);
        if (driver.getTitle().contains(windowTitle)) {
            windowExists = true;
            logger.info(windowTitle
                    + " Title window exists, Switched to new window");
            break;
        }
    }
    if (!windowExists) {
        Assert.fail(windowTitle + " Title window not exists");
    }
}

Wednesday, 16 December 2015

Page UP/DOWN, Scroll, Move to Element in Selenium


// Moving cursor in top left corner using JavascriptExecutor

((JavascriptExecutor) driver).executeScript("window.scrollTo(0, 0);");

Ref: https://developer.mozilla.org/en-US/docs/Web/API/Window/scroll

// Page UP Using Keys
@FindBy(xpath = "//button[contains(text(),'Save')]")
public WebElement saveButton;

saveButton.sendKeys(Keys.PAGE_UP);


// Move to Element Using Actions.

@FindBy(xpath = "//button[contains(text(),'Save')]")
public WebElement saveButton;

Actions actions = new Actions(driver);
actions.moveToElement(saveButton);
actions.perform(); 

Ref: https://selenium.googlecode.com/git/docs/api/java/org/openqa/selenium/interactions/Actions.html#moveByOffset-int-int-

// Move to Specific  Element Using JavascriptExecutor.

@FindBy(xpath = "//button[contains(text(),'Save')]")
public WebElement saveButton;

1) 
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", saveButton); 

2)
((JavascriptExecutor) driver).executeScript("window.scrollTo(0,"
saveButton.getLocation().y + ")");



Tuesday, 8 December 2015

Print DropDown Menu in Selenium + MouseOver in Selenium

How should we print DropDown Menu list which appear on MouseOver?




Sample Code:


// Open Browser
WebDriver driver = new FirefoxDriver();
// Open WebSite
driver.get("http://www.amazon.in/");
// WebElement os 'Shop By Category'
WebElement shopBy = driver.findElement(By.xpath(".//*[@id='nav-link-shopall']/span[2]"));// Actions as we have to do mouse event .. Need to move mouse over to 'Shop By Category'.
Actions actions = new Actions(driver);
actions.moveToElement(shopBy).build().perform();
// Runtime, we get all Navigation element, so if any new element added then also we can get that element in print list.
List<WebElement> navigations = driver.findElements(By.xpath("//*[@id='nav-flyout-shopAll']/div[@class='nav-template nav-flyout-content nav-tpl-itemList']/span[@role='navigation']/span[@class='nav-text']"));
for(WebElement navigation : navigations)
System.out.println(navigation.getText());
// Close Browser

driver.close();


Saturday, 5 December 2015

Set Browser language in Selenium

Chrome Browser -

        System.setProperty("webdriver.chrome.driver","<LOCATION OF CHROME DRIVER EXE FILE>");
        ChromeOptions options = new ChromeOptions();
        options.addArguments(“–lang= sl”);
        ChromeDriver driver = new ChromeDriver(options);
  
Firefox Browser -

        FirefoxProfile profile = new FirefoxProfile();
        profile.setPreference(“intl.accept_languages”,”fr”);
        driver = new FirefoxDriver(profile);


Type in Rich Text Editor in Selenium

How to type in Rich Text Editor that is located in iFrame ?

public void typeInRichTextEditor() {
        String baseURL = "http://www.speurders.nl/plaatsen";
        WebDriver driver = new FirefoxDriver();
        driver.get(baseURL);
        ((JavascriptExecutor) driver).executeScript("window.scrollTo(0,"
                + driver.findElement(
                        By.xpath("//iframe[@id='description_ifr']")).getLocation().y + ")");
        WebElement descriptionElement = driver.findElement(By
                .xpath("//iframe[@id='description_ifr']"));
        driver.switchTo().frame(descriptionElement);

        WebElement editable = driver.switchTo().activeElement();
        editable.sendKeys("Anand Somani");
        driver.switchTo().defaultContent();
        driver.quit();
    }