In this post we will see more Webdriver actions that we can perform using webdriver as continuation to earlier post.
RightClick Action
Action to be performed on web-element right click. Find the sample to perform right click and print the status .
public void rightclick(By by) throws Throwable { boolean status = false; try { WebElement elementToRightClick = driver.findElement(by); Actions rightclick = new Actions(driver); rightclick.contextClick(elementToRightClick).perform(); status = true; } catch (Exception e) { } finally { if (status) System.out.println("RightClick Success"); else System.out.println("RightClick Failed"); } }
Drop-Down selection action
Find the sample to select a drop-down by value in the below code snippet and print the status to the console. We can perform the select operation by index and by visibleText also.
public void selectDropDownByValue(By locator, String value) throws Throwable { boolean status = false; try { Select s = new Select(driver.findElement(locator)); s.selectByValue(value); status = true; } catch (Exception e) { } finally { if (status) { System.out.println("Select Success"); } else { System.out.println("Select Fail"); } } }
Select Dropdown by Visible Text :
public void selectDropDownByVisibleText(By locator, String visibletext) throws Throwable { boolean status = false; try { Select s = new Select(driver.findElement(locator)); s.selectByVisibleText(visibletext); status = true; } catch (Exception e) { } finally { if (status) { System.out.println("Select Success "); } else { System.out.println("Select Fail "); } } }
Switch to a frame by index
public void switchToFrameByIndex(int index) throws Throwable { boolean status = false; try { new WebDriverWait(driver, 30) .until(ExpectedConditions. visibilityOfElementLocated(By.xpath("//iframe"))); driver.switchTo().frame(index); status = true; } catch (Exception e) { } finally { if (status) { System.out.println("SwitchFrame Success "); } else { System.out.println("SelectFrame Failure"); } } }
Note: Please note that we can also perform the switch operation by frame ID, name, and default content.
driver.switchTo().defaultContent();
To slide an object :
Method to slide an object to some distance and print status to console.
public void slideAction(By slider, int x, int y) throws Throwable { boolean status = false; try { WebElement dragitem = driver.findElement(slider); new Actions(driver) .dragAndDropBy(dragitem, x, y) .build() .perform(); status = true; } catch (Exception e) { } finally { if (status) { System.out.println("Slide action success "); } else { System.out.println("Slide action fail "); } } }
JavaScript Execute
A simple way to execute java-script :
((JavascriptExecutor) driver).executeScript(script);
Some boolean methods are useful in WebDriver automation.
To check alert present or not
public boolean isAlertPresent() { try { driver.switchTo().alert(); return true; } catch (NoAlertPresentException ex) { return false; } }
Element visible or not :
driver.findElement(locator).isDisplayed();
How to get page source :
driver.getPageSource()
–
Selenium Tutorial on this website:
https://www.testingdocs.com/selenium-webdriver-tutorial/
More information on Selenium, visit official website: