Selenium WebDriver

  • Action
    By by = By.xpath("");
    
    Actions action = new Actions(driver);
    WebElement elementToHoverOver = driver.findElement(by);
    action.moveToElement(elementToHoverOver);
    action.perform();
    this.sleep(2);
    
    WebElement element = driver.findElement(By.name("source"));
    WebElement target = driver.findElement(By.name("target"));
    
    (new Actions(driver)).dragAndDrop(element, target).perform();

    Drag And Drop Selenium Doc

  • Interface WebDriver.TargetLocator (Java Doc)
    Switches to the element that currently has focus within the document currently "switched to", or the body element if this cannot be detected.

    WebElement activeElement = getCurrentDriver().switchTo().activeElement();
    activeElement.click();
    Alert alert = driver.switchTo().alert();
    
    alert.dismiss();
    alert.accept();
    From time to time you facing iframes in web applications, usually if you trying to interact right away, well your script is failing, why would it fail?

    Well in order to work inside iframe you have to switch to it:

    driver.switchTo().frame("iframe name");

    At some point when you finish working in iframe, you would need to switch to default content back:

    driver.switchTo().defaultContent();
    driver.switchTo().frame(index)
    From time to time you facing iframes in web applications, usually if you trying to interact right away, well your script is failing, why would it fail?

    Well in order to work inside iframe you have to switch to it:

    driver.switchTo().frame("iframe name");

    At some point when you finish working in iframe, you would need to switch to default content back:

    driver.switchTo().defaultContent();
    From time to time you will need to switch in between frames, here is how you do it:

    driver.switchTo().frame("frameName");
    Well sometimes you need to navigate through windows on the screen here is how you do it:

    driver.switchTo().window("windowName");
    
    for (String handle : driver.getWindowHandles()) {
        driver.switchTo().window(handle);
    }
  • public interface WebElement (Java Doc)
    When you fill up any type of form, before you sendKeys(), make sure that input field is clear, by performing next:

     driver.findElement(By.id("username").clear();
    Well most of the time when you interacting page you will click on something, here is how you do it with Selenium:

       driver.findElment(By.id("login-button").click();
    todo: findElement(By by)
    todo: findElements(By by)
    Sometimes you would want to assert that attribute was not changed or corrupted, here is how you would do it:

    WebElement DownloadURL = driver.findElement(By.link_text("Download URL"))
    String urlToCompare = DownloadURL.getAttribute("href").toString();
    
    Assert.assertEquals("URL_To_Compare", urlToCompare);
    From time to time you would need to getCssValue of particular element on the page. For example in my current project when I click on tab it is changing background color, so whenever tab is active I'm asserting that background-color for particular tab changed. Here is how you do it:

    WebElement cssElement = driver.findElement(By.id("elementID"));
    
    assertEquals("rgba(243, 242, 235, 1)", cssElement.getCssValue("background-color"));
    assertEquals("238px", cssElement.getCssValue("width"));
    assertEquals("absolute", cssElement.getCssValue("position"));
    assertEquals("0.9", cssElement.getCssValue("opacity"));
    You would use getLocation() when you need to find exact element location on the page.

    WebElement component = driver.findElement(By.cssSelector("#cq-gen105"));
    
    int x = component.getLocation().getX();
    int y = component.getLocation().getY();
    

    Here more detailed post how to perform click on element with specific coordinate.

    Let say you need to submit avatar for your profile and make sure it is resized correct, so it is rendering on the screen as it suppose, well in that case you can use getSize() and then assert height and width of the image, here is how you do it

    Dimension image =driver.findElement(By.id("test-image")).getSize();  
    assertEquals("256", image.width);  
    assertEquals("256", image.height); 
    WebElement frame = driver.findElement(By.tagName("iframe"));
    From time to time you want make sure that text on the page was not changed by any chance, here is how you would do that:

    WebElement textLocation = driver.findElement(By.id("importantText"));
    assertEquals(textLocation.getText(), "Here is some important text");

    In recent versions of WebDriver engineers experienced problems using getText() with title, if you have same problem you might use getAttribute(“text”).

    In my current project I have to expand calendar and change the date in it, the problem depend on the things I have being doing before, calendar sometimes expanded sometimes not. So I'm using isDisplayed() method to validate if calendar displayed or no on the page, and if it is not I click on the expand button.

    if(!driver.getElment(By.id("some_id_here")).isDisplayed()){
                        driver.getElment(By.id("another_id_here")).click();
                    } else {
                        System.out.println("The Element was expanded");
                    }
    One of the case when we use isSelected() is check boxes, here is an example:

    WebDriver driver = new FirefoxDriver();
    
    first_checkBox = driver.findElement(By.id("first_checkBox"));
    second_checkBox = driver.findElement(By.id("second_checkBox"));
    
    if(first_checkBox.isSelected){
    second_ceckBox.click();
    }
    Well there is plenty input fields on the web, here how you send keys to input field

    WebDriver driver = new FirefoxDriver();
    
    driver.findElement(By.id("email")).sendKeys(Keys.ENTER);
    We all submit forms, here is how you do it with WebDriver:

    WebDriver driver = new FirefoxDriver();
    
    driver.findElement(By.id("submiBtn")).submit();
  • public static interface WebDriver.Navigation (Java Doc)
  • Interface WebDriver.Options (Java Doc
    addCookie(Cookie cookie)
    deleteAllCookies()
    deleteCookie(Cookie cookie)
    deleteCookieNamed(java.lang.String name)
    getCookieNamed(java.lang.String name)
    getCookies()
    todo: ime()
    todo: logs()
    todo: timeouts()
  • todo: window()
  • Driver manager
    todo: 1
    todo: 2
    todo: 3
  • Drivers
    todo: 1
    todo: 2
    todo: 3
  • Frameworks
    • Data Driven
      todo: 1
      todo: 2
      todo: 3
    • Keyword Driven
      todo: 1
      todo: 2
      todo: 3
    • Hybrids
      todo: 1
      todo: 2
      todo: 3
    • Page Objects (wiki) / Page Factory (wiki)
      todo: 1
      todo: 2
      todo: 3
  • JavaScript
  • Test Runners
  • UI locators (documentation)
    XPATH
    ID
    todo: ID_OR_NAME
    todo: NAME
    todo: CLASS_NAME
    todo: CSS
    todo: LINK_TEXT
    todo: PARTIAL_LINK_TEXT
    todo: TAG_NAME
  • Wait