TestingDocs.com
    Software Testing website
    • Automation
      • Selenium
      • JBehave Framework
    • Tutorials
      • MySQL Tutorials
      • Testlink
      • Maven
      • Git
    • IDEs
      • IntelliJ IDEA
      • Eclipse
    • Flowcharts
      • Flowgorithm
      • Raptor
    • About

    Selenium

    Learning WebDriver Actions

    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:

    https://www.selenium.dev/

    Related Posts

    Windows 10 Settings

    Selenium /

    Add Microsoft Webdriver on Windows OS

    Download Selenium Components

    Selenium /

    Getting Started with Selenium Webdriver

    LambdaTest Testing Cloud SaaS Platform

    Selenium /

    LambdaTest – Testing Cloud SaaS Platform

    Selenium /

    Selenium 3.0 and Mozilla GeckoDriver

    SauceLabs Website

    Selenium /

    Run an Example Test on SauceLabs

    ‹ WebDriver for Microsoft Edge Browser› Learning Selenium WebDriver

    Recent Posts

    • ChatGPT Plans Free and PlusChatGPT Subscription Plans
    • Stellar Converter for Database ToolStellar Converter for Database
    • Stellar MySQL Log AnalyzerStellar Log Analyzer for MySQL
    • Stellar Repair for MySQLStellar Repair for MySQL
    • ChatGPT IntroductionChatGPT Capabilities
    • How to secure your SQL Database: Tips and Tricks
    • ChatGPT4 Conversational AI FeaturesChatGPT4 Conversational AI Features
    • Trends in Software EngineeringShaping the Future of Development: Exploring Key Trends in Software Engineering
    • Java PerformanceImproving Java Performance with Multithreading
    • QDrant Vector DatabaseOpen-source Vector Databases
    • Difference between PHP and JavaScript?
    • Bing AI Browser Web ContentBing Conversation Styles
    • ChatGPT PreviewChatGPT Introduction
    • Open Source AI Frameworks TensorFlowOpen Source AI Frameworks
    • Artificial Intelligence Tools

    Back to Top

    Links

    • Contact
    • Privacy Policy
    • Cookie Policy

    www.TestingDocs.com