Learning WebDriver Actions 2

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 Drop down 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 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()

Related Posts

AndroidDeveloperOptions

Appium, Selenium

ADB(Android Debug Bridge) Setup on Windows

Chrome Selenium IDE Download

Selenium

Selenium IDE for Chrome browser

Maven_Archtype_2

Apache Maven, Selenium

Maven Archetype

EventFiringWebDriver_Listener

Selenium

EventFiringWebDriver

Timeouts-Interface-Selenium

Selenium

Timeouts Interface in WebDriver API

Selenium

JSON wire protocol

Navigation

  • Home
  • Selenium
  • JBehave
  • Questions
  • Contact
  • Privacy
  • About

Random Posts

  • List_OperationsJava List Interface Example
  • Test webpage urlTesting webpage mobile-friendliness using a tool
  • Timeout test JUnitTesting timeouts with JUnit 4
  • JUnit Jupiter APIJUnit 5 Jupiter API Annotations
  • JUnit LibraryAdding JUnit5 library to a Project
  • Serenity BDD JBehave Result JenkinsRunning Serenity BDD stories with Jenkins
  • SerenityReportDebugDebug Serenity BDD JBehave project
  • Git Commit JBehave ProjectAdding Stories to JBehave project
  • Filtering stories JBehave MavenFiltering Stories in JBehave
  • Serenity Project Information 2Create Serenity JBehave project from command line.
  • Screenshot Test StepRunning Serenity Tests
  • No goals specified Maven ErrorHow to fix Maven error No goals specified
  • Sample_Eclipse_Check_programAutomation Environment Setup Verification
  • Blue Ocean UIBlue Ocean Jenkins Plugin
  • Android Virtual Device ManagerCreating Android Virtual Device with AVD Manager
TestingDocs.com
© 2016 - 2019 TestingDocs.com All Rights Reserved
TestingDocs.com

TestingDocs.com

Links

  • Home
  • Selenium
  • JBehave
  • Questions
  • Contact
  • Privacy
  • About

Search