Saturday, October 7, 2017

Useful Selenium code need to know


Refresh Page

driver.navigate().refresh();

Navigate On The Browser

driver.navigate().back();
driver.navigate().forward();

Focus On A Textbox Element

WebElement element  = driver.findElement(By.id("element-id"));
element.sendKeys("");

Overwrite On Textbox

WebElement element = driver.findElement(By.id("element-id"));
element.clear();
element.sendKeys("new input value");

Check If An Element Is Visible

WebElement element  = driver.findElement(By.id("element-id"));
if(element instanceof RenderedWebElement) {
System.out.println("Element visible");
} else {
System.out.println("Element Not visible");
}

Check If An Element Exists

driver.findElements(By.id("element-id")).size()!=0

Wait For Element To Be Available


WebDriverWait wait = new WebDriverWait(driver, 30);
WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.id("id123")));

 

Mouseover Action To Make Element Visible Then Click

Actions actions = new Actions(driver);
WebElement menuElement = driver.findElement(By.id("menu-element-id"));
actions.moveToElement(menuElement).moveToElement(driver.findElement(By.xpath("xpath-of-menu-item-element"))).click();