Created
October 27, 2016 01:33
-
-
Save shivanshu3/ee5ba88996efb27af78db422be51555d to your computer and use it in GitHub Desktop.
This is a Selenium demo for CPEN422 using an assignment from CPEN400A
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package selenium.presentation; | |
import java.lang.reflect.Type; | |
import java.util.Map; | |
import org.openqa.selenium.By; | |
import org.openqa.selenium.JavascriptExecutor; | |
import org.openqa.selenium.WebDriver; | |
import org.openqa.selenium.WebElement; | |
import org.openqa.selenium.interactions.Actions; | |
import org.openqa.selenium.chrome.ChromeDriver; | |
import com.google.gson.Gson; | |
import com.google.gson.reflect.TypeToken; | |
public class Main { | |
public static void main(String[] args) throws InterruptedException { | |
// The Chrome driver supports javascript | |
System.setProperty("webdriver.chrome.driver", "C:\\Users\\Shivanshu\\Downloads\\chromedriver_win32\\chromedriver.exe"); | |
WebDriver driver = new ChromeDriver(); | |
Actions builder = new Actions(driver); | |
JavascriptExecutor js = (JavascriptExecutor)driver; | |
// Go to the home page | |
driver.get("file:///C:/Users/Shivanshu/Documents/CPEN400A-2016-L1A-Group3/assignment-2/index.html"); | |
Thread.sleep(3000); | |
// Jeans add button: | |
WebElement jeansAddButton = driver.findElement(By.cssSelector("#Jeans .add")); | |
WebElement jeansDiv = driver.findElement(By.id("Jeans")); | |
builder.moveToElement(jeansDiv).perform(); | |
jeansAddButton.click(); | |
Thread.sleep(3000); | |
driver.switchTo().alert().accept(); | |
// Keyboard add button: | |
WebElement keyboardAddButton = driver.findElement(By.cssSelector("#Keyboard .add")); | |
WebElement keyboardDiv = driver.findElement(By.id("Keyboard")); | |
builder.moveToElement(keyboardDiv).perform(); | |
keyboardAddButton.click(); | |
Thread.sleep(3000); | |
driver.switchTo().alert().accept(); | |
// State verification: | |
String cartJson = (String) js.executeScript("return JSON.stringify(window.cart);"); | |
Gson gson = new Gson(); | |
Type hashMapType = new TypeToken<Map<String, Integer>>(){}.getType(); | |
Map<String, Integer> cart = gson.fromJson(cartJson, hashMapType); | |
Integer numJeans, numKeyboards, numMice; | |
numJeans = cart.get("Jeans"); | |
numKeyboards = cart.get("Keyboard"); | |
numMice = cart.get("Mice"); | |
System.out.println("#Jeans: " + numJeans); | |
System.out.println("#Keyboards: " + numKeyboards); | |
System.out.println("#Mice: " + numMice); | |
assert numJeans == 1; | |
assert numKeyboards == 1; | |
assert numMice == null; | |
System.out.println("All tests passed!"); | |
driver.quit(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment