Skip to content

Instantly share code, notes, and snippets.

@sauceaaron
Last active September 26, 2019 17:32
Show Gist options
  • Save sauceaaron/d9ba0823e477ef0fbea9815b2d031088 to your computer and use it in GitHub Desktop.
Save sauceaaron/d9ba0823e477ef0fbea9815b2d031088 to your computer and use it in GitHub Desktop.
Select the first or last item on a menu, or sort by price and select the most expensive. Or just order the lobster.
<html>
<head>
<title>Menu</title>
<style>
button:focus { background-color:red;}
</style>
</head>
<body>
<h1> Menu </h1>
<ul id="menu">
<li class="entree"> <button id="lobster">Lobster $29.99</button> </li>
<li class="entree"> <button id="steak">Steak $18.99</button> </li>
<li class="entree"> <button id="salad">Salad $7.99</button> </li>
<li class="entree"> <button id="chicken">Chicken $12.99</button> </li>
</ul>
</body>
</html>
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import java.util.Comparator;
import java.util.List;
public class MenuSelectionTest
{
WebDriver driver;
@Test
public void orderTheLobster()
{
WebElement lobster = driver.findElement(By.id("lobster"));
System.out.println(lobster.getText());
lobster.click();
}
@Test
public void orderTheLowestPricedMenuItem()
{
List<WebElement> elements = driver.findElements(By.cssSelector("#menu > .entree > button"));
WebElement leastExpensive = elements.stream().sorted(sortByPrice).findFirst().get();
System.out.println(leastExpensive.getText());
leastExpensive.click();
}
@Test
public void orderTheHighestPricedMenuItem()
{
List<WebElement> elements = driver.findElements(By.cssSelector("#menu > .entree > button"));
WebElement mostExpensive = elements.stream().sorted(sortByPrice.reversed()).findFirst().get();
System.out.println(mostExpensive.getText());
mostExpensive.click();
}
@Test
public void orderTheFirstMenuItem()
{
WebElement firstItem = driver.findElement(By.cssSelector("#menu > .entree:first-of-type > button"));
System.out.println(firstItem.getText());
firstItem.click();
}
@Test
public void orderTheLastMenuItem()
{
WebElement lastItem = driver.findElement(By.cssSelector("#menu > .entree:last-of-type > button"));
System.out.println(lastItem.getText());
lastItem.click();
}
@Before
public void setup()
{
driver = new ChromeDriver();
driver.get("https://sauceaaron.github.io/MenuSelection/menu.html");
}
@After
public void teardown() throws InterruptedException
{
Thread.sleep(5*1000);
driver.quit();
}
Comparator<WebElement> sortByPrice = new Comparator<WebElement>() {
@Override
public int compare(WebElement a, WebElement b)
{
System.out.println("comparing " + a.getText() + " to " + b.getText());
return getPrice(a).compareTo(getPrice(b));
}
};
public Double getPrice(WebElement item)
{
return Double.valueOf(item.getText().split("\\$")[1].trim());
}
}
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>oneshore.example</groupId>
<artifactId>MenuSelection</artifactId>
<version>1.0</version>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>8</source>
<target>8</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.141.59</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment