Skip to content

Instantly share code, notes, and snippets.

@pragmatictesters
Created December 31, 2018 19:43
Show Gist options
  • Save pragmatictesters/554509b90d6efb41afb16be06a705d55 to your computer and use it in GitHub Desktop.
Save pragmatictesters/554509b90d6efb41afb16be06a705d55 to your computer and use it in GitHub Desktop.
Selenium WebDriver Examples : Move , Scroll to an element using Actions class and JavascriptExecutor
package com.pragmatic.selenium.examples;
import io.github.bonigarcia.wdm.WebDriverManager;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import java.util.concurrent.TimeUnit;
/**
* This class demonstrates reading test data from a CSV file
**/
public class MoveToElementExample {
private static final String BASE_URL = "http://demoshop.pragmatictestlabs.com";
private WebDriver driver;
@BeforeClass
public void beforeClass() {
WebDriverManager.chromedriver().setup();
}
@BeforeMethod
public void beforeMethod() {
driver = new ChromeDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.get(BASE_URL);
}
@AfterMethod
public void afterMethod() throws Exception {
Thread.sleep(5000);
driver.close();
}
@Test
public void testMoveToElementActionsClass() {
WebElement product = driver.findElement(By.xpath("//a[@data-product_id='27']"));
Actions actions = new Actions(driver);
actions.moveToElement(product)
.click();
}
@Test
public void testScrollToElementJavascriptExecuter() {
WebElement product = driver.findElement(By.xpath("//a[@data-product_id='27']"));
((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(true);", product);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment