Вы находитесь на странице: 1из 5

package intro;

import static org.testng.Assert.assertEquals;

import java.sql.Array;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

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.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.Assert;
import org.testng.Reporter;
import org.testng.annotations.Test;

public class temper {

@Test
public void temperature() {

try {

System.setProperty("webdriver.chrome.driver",
"C:\\selenium\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("http://weathershopper.pythonanywhere.com/");

Reporter.log("Browser Invoked and navigated to URL", true);

String temp =
driver.findElement(By.id("temperature")).getText();
String[] temp_split = temp.split(" ");
int numb = Integer.parseInt(temp_split[0]);
System.out.println(numb);

if (numb>34) {

driver.findElement(By.xpath("//button[contains(text(),'Buy
sunscreens')]")).click();
System.out.println("checkpoint2");
String[] itemsNeeded = { "SPF-30", "SPF-50" };
System.out.println("checkpoint_sunscreen");
mapping(driver, itemsNeeded);
}
else if (numb<19) {

driver.findElement(By.xpath("//button[contains(text(),'Buy
moisturizers')]")).click();
String[] itemsNeeded = { "Almond", "Aloe" };
System.out.println("checkpoint_moisturizer");
mapping(driver, itemsNeeded);
}
else {
System.out.println("Temperature is normal. Stay
hydrated.");
}
}
catch (Exception e) {
e.printStackTrace();
}
}

public void mapping(WebDriver driver, String[] items) throws


InterruptedException {

// Fetch All the Products Text


List<WebElement> list_of_products =
driver.findElements(By.cssSelector("p.font-weight-bold.top-space-10"));
List<WebElement> list_of_products_price =
driver.findElements(By.cssSelector("p.font-weight-bold.top-space-10~p"));
List<WebElement> buttons =
driver.findElements(By.cssSelector("p.font-weight-bold.top-space-10~p~button"));

// Use of HashMap to store Products and Their prices(after


conversion to Integer)
String product_name;
String product_price;
int int_product_price;
int low_price;
HashMap<Integer, String> map_final_products = new
HashMap<Integer, String>();
HashMap<Integer, String> map_final_products_pro = new
HashMap<Integer, String>();
//LinkedHashMap<Integer, String> map_final_products_pro_check =
new LinkedHashMap<Integer, String>();
ArrayList<String> al = new ArrayList<String>();

for(int i=0;i<list_of_products.size();i++) {
product_name = list_of_products.get(i).getText();//Iterate
and fetch product name
product_price =
list_of_products_price.get(i).getText();//Iterate and fetch product price
product_price = product_price.replaceAll("[^0-9]",
"");//Replace anything with space other than numbers
int_product_price =
Integer.parseInt(product_price);//Convert to Integer

map_final_products.put(int_product_price,product_name);//Add product and


price in HashMap
}

Reporter.log("Product Name and price fetched from UI and saved in


HashMap as:" + map_final_products.toString(),true);

for(int j = 0; j < items.length; j++) {


map_final_products_pro.clear();
for (int k=0; k < list_of_products.size(); k++) {

if((list_of_products.get(k).getText().toLowerCase()).contains(items[j].toLowerCase(
))) {

map_final_products_pro.put(Integer.parseInt(list_of_products_price.get(k).getText()
.replaceAll("[^0-9]", "")), list_of_products.get(k).getText());
}
}

//Reporter.log("Product Name and price fetched from UI and


saved in HashMap as:" + map_final_products_pro.toString(),true);
Set<Integer> allkeys = map_final_products_pro.keySet();
ArrayList<Integer> array_list_values_product_prices = new
ArrayList<Integer>(allkeys);
Collections.sort(array_list_values_product_prices);
low_price = array_list_values_product_prices.get(0);
//Reporter.log("Low Product Price is: " + low_price + "
Product name is: " + map_final_products_pro.get(low_price),true);
//map_final_products_pro_check.put(low_price,map_final_prod
ucts_pro.get(low_price));
al.add(map_final_products_pro.get(low_price));
al.add(Integer.toString(low_price));
//Reporter.log("Product Name and price saved in HashMap
as:" + map_final_products_pro_check.toString(),true);

for(int i=0;i<list_of_products.size();i++) {

if(map_final_products_pro.get(low_price).equalsIgnoreCase(list_of_products.get(i).g
etText())){
buttons.get(i).click();
}
}
}
Thread.sleep(5000);
addToCart(driver,items,al);

public void addToCart(WebDriver driver,String[] items,


ArrayList<String> al) throws InterruptedException {

driver.findElement(By.cssSelector("button.thin-text.nav-
link")).click();
WebDriverWait w = new WebDriverWait(driver, 10);

w.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//h2[contains(text(
),'Checkout')]")));
int k=0;
for(int i=1;i <= items.length; i++) {
for(int j=1;j<=2;j++) {
WebElement web =
driver.findElement(By.xpath("//table[@class='table table-
striped']//tbody//tr["+i+"]//td["+j+"]"));
if(web.getText().toString().equalsIgnoreCase((String)al.toArray()[k])) {
System.out.println(web.getText().toString()+"
verified");
//assert.assertEquals(actual, expected);
//verify the sum of the prices of products
}
else {
System.out.println("not verified");
}
k++;
}
}
cardPayment(driver);
}

public void cardPayment(WebDriver driver) throws InterruptedException


{
driver.findElement(By.xpath("//button[@class='stripe-button-
el']//span[contains(text(),'Pay with Card')]")).click();

driver.switchTo().frame(0);
WebDriverWait block = new WebDriverWait(driver,10);
WebElement modal =
block.until(ExpectedConditions.visibilityOfElementLocated(By.className("Modal")));

modal.findElement(By.xpath("//input[@placeholder='Email']")).sendKeys("qwerty@gmail
.com");
Thread.sleep(9000);
System.out.println("outside loop");
if(driver.findElement(By.cssSelector("div.Codebox.can-
setfocus.Section-child")).isDisplayed()) {
System.out.println("inside classname loop");
Thread.sleep(9000);
if(driver.findElement(By.className("CodeNotReceived-
actionMessage")).isDisplayed()){
System.out.println("inside inside classname loop");
driver.findElement(By.className("CodeNotReceived-
actionMessage")).click();
}
}
Thread.sleep(5000);
modal.findElement(By.xpath("//input[@placeholder='Card
number']")).sendKeys("4242424242424242");
modal.findElement(By.xpath("//input[@placeholder='MM /
YY']")).sendKeys("09/23");

modal.findElement(By.xpath("//input[@placeholder='CVC']")).sendKeys("363");
modal.findElement(By.xpath("//input[@placeholder='ZIP
Code']")).sendKeys("689121");
modal.findElement(By.className("Checkbox")).click();

modal.findElement(By.xpath("//input[@inputmode='tel']")).sendKeys("9876543210");
modal.findElement(By.xpath("//button[@type='submit']")).click();

paymentSuccess(driver);
}

public void paymentSuccess(WebDriver driver) throws


InterruptedException {
Thread.sleep(9000);
WebElement heading =
driver.findElement(By.xpath("/html[1]/body[1]/div[1]/div[1]/h2[1]"));
if(heading.getText() != null){
if(heading.getText().equalsIgnoreCase("Payment Success")) {

System.out.println(driver.findElement(By.cssSelector("p.text-
justify")).getText());
driver.close();
}
else {
System.out.println("Oops! Payment Failed");
}
}
}
}

Вам также может понравиться