Skip to content

Instantly share code, notes, and snippets.

@dmitriyvolk
Created November 17, 2023 05:57
Show Gist options
  • Save dmitriyvolk/6d1de8a694a3ac0c59667a3eb94cf921 to your computer and use it in GitHub Desktop.
Save dmitriyvolk/6d1de8a694a3ac0c59667a3eb94cf921 to your computer and use it in GitHub Desktop.
Selenium: Select and Action
import org.openqa.selenium.By;
import org.openqa.selenium.Dimension;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;
import org.testng.Assert;
import org.testng.annotations.Test;
import school.redrover.runner.BaseTest;
/*
* Ignore the `BaseTest` part
*/
public class ActionTest extends BaseTest {
private Actions actions;
private Actions getActions() {
if (actions == null) {
actions = new Actions(getDriver());
}
return actions;
}
void goToWebForm() {
// goes to https://www.selenium.dev/selenium/web/web-form.html
getDriver().findElement(By.linkText("web-form.html")).click();
}
void goToDragAndDrop() {
// goes to https://www.selenium.dev/selenium/web/dragAndDropTest.html
getDriver().findElement(By.linkText("dragAndDropTest.html")).click();
}
@Test
void testSliderArrowKeys() throws Exception {
goToWebForm();
WebElement slider = getDriver().findElement(By.name("my-range"));
slider.sendKeys(Keys.LEFT, Keys.LEFT, Keys.LEFT, Keys.LEFT);
Assert.assertEquals("1", slider.getAttribute("value"));
Thread.sleep(5000);
}
@Test
void testSliderAction() throws Exception {
goToWebForm();
WebElement slider = getDriver().findElement(By.xpath("//input[@name='my-range']"));
getActions().clickAndHold(slider)
.moveByOffset(-200, 0)
.release()
.perform();
System.out.println(slider.getAttribute("value"));
Assert.assertEquals("0", slider.getAttribute("value"));
Thread.sleep(5000);
}
@Test
void testSliderClick() throws Exception {
goToWebForm();
WebElement slider = getDriver().findElement(By.name("my-range"));
final Dimension size = slider.getSize();
int sliderWidth = size.getWidth();
getActions().moveToElement(slider)
.moveByOffset(sliderWidth * 2 / 5, 0)
.click()
.perform();
Thread.sleep(5000);
}
@Test
void testKeyActions() throws Exception {
goToWebForm();
WebElement textArea = getDriver().findElement(By.name("my-textarea"));
getActions().click(textArea)
.keyDown(Keys.SHIFT)
.sendKeys("abCDe")
.keyUp(Keys.SHIFT)
.perform();
Assert.assertEquals("ABCDE", textArea.getAttribute("value"));
Thread.sleep(5000);
}
@Test
void testDragAndDrop() throws Exception {
goToDragAndDrop();
WebElement test1 = getDriver().findElement(By.id("test1"));
getActions().clickAndHold(test1)
.moveByOffset(100, 50)
.pause(1000)
.moveByOffset(100, 50)
.release()
.perform();
Thread.sleep(5000);
}
@Test
void testDragAndDropBy() throws Exception {
goToDragAndDrop();
WebElement test1 = getDriver().findElement(By.id("test1"));
getActions()
.dragAndDropBy(test1, 200, 400)
.perform();
Thread.sleep(2000);
}
@Test
void testDragAndDropToElement() throws Exception {
goToDragAndDrop();
WebElement test1 = getDriver().findElement(By.id("test1"));
System.out.println(test1.getLocation());
WebElement test4 = getDriver().findElement(By.id("test4"));
getActions()
.dragAndDrop(test1, test4)
.perform();
System.out.println(test1.getLocation());
Assert.assertEquals(test4.getLocation(), test1.getLocation());
Thread.sleep(2000);
}
@Test
void testColor() throws Exception {
goToWebForm();
final WebElement colorPicker = getDriver().findElement(By.name("my-colors"));
System.out.println(colorPicker.getAttribute("value"));
colorPicker.click();
getActions()
.sendKeys(Keys.TAB, Keys.TAB, Keys.TAB)
.sendKeys("255")
.sendKeys(Keys.TAB)
.pause(500)
.sendKeys("0")
.sendKeys(Keys.TAB)
.pause(500)
.sendKeys("0")
.pause(500)
.sendKeys(Keys.ENTER)
.perform();
final String color = colorPicker.getAttribute("value");
System.out.println(color);
Assert.assertEquals("#ff0000", color);
Thread.sleep(2000);
}
}
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.Select;
import org.testng.Assert;
import org.testng.annotations.Test;
import school.redrover.runner.BaseTest;
import java.util.stream.Collectors;
/*
* Please ignore the `BaseTest` part
*/
public class SelectTest extends BaseTest {
void goToSelectPage() {
//goes to https://www.selenium.dev/selenium/web/selectPage.html
getDriver().findElement(By.linkText("selectPage.html")).click();
}
@Test
public void testSimpleDropDown() throws Exception {
goToSelectPage();
final WebElement selectWithoutMultiple = getDriver().findElement(By.id("selectWithoutMultiple"));
Select simpleDropDown = new Select(selectWithoutMultiple);
simpleDropDown.selectByValue("two");
String newValue = selectWithoutMultiple.getAttribute("value");
System.out.println(newValue);
Assert.assertEquals("two", newValue);
Thread.sleep(5000);
}
@Test
public void testMultipleSelect() throws Exception {
goToSelectPage();
WebElement selectElement = getDriver().findElement(By.id("selectWithMultipleEqualsMultiple"));
Select multiSelect = new Select(selectElement);
// First Option is selected already via HTML.
multiSelect.deselectByIndex(0);
multiSelect.selectByIndex(2);
multiSelect.selectByVisibleText("Cheddar");
System.out.println(selectElement.getAttribute("value"));
System.out.println(multiSelect.getAllSelectedOptions().stream().map(WebElement::getText).collect(Collectors.toList()));
Thread.sleep(5000);
}
@Test
void testLongList() throws Exception {
goToSelectPage();
WebElement selectElement = getDriver().findElement(By.id("selectWithMultipleLongList"));
Select select = new Select(selectElement);
select.selectByVisibleText("five");
select.selectByVisibleText("six");
System.out.println(selectElement.getAttribute("value"));
System.out.println(select.getAllSelectedOptions().stream().map(WebElement::getText).collect(Collectors.toList()));
Thread.sleep(5000);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment