Code Menu
Code Menu
import java.util.ArrayList;
import java.util.Collections;////////code of menu...before passing browser name.
import java.util.HashMap;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.InvalidSelectorException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.edge.EdgeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.Reporter;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
import objectRepository.objectsClass;
//Page Object Factory and Page Object Pattern both are used to improve
simplicity and readability of the code
WebDriver driver;
@BeforeTest
@Parameters("browser")
if (browser.equalsIgnoreCase("Edge")) {
String windir = System.getenv("windir");
String edgeDriverPath = windir +
"\\SysWOW64\\MicrosoftWebDriver.exe";
System.setProperty("webdriver.edge.driver", edgeDriverPath);
driver = new EdgeDriver();
}
else if (browser.equalsIgnoreCase("Chrome")) {
System.setProperty("webdriver.chrome.driver",
"C:\\selenium\\chromedriver.exe");
driver = new ChromeDriver();
}
else if (browser.equalsIgnoreCase("Firefox")) {
System.setProperty("webdriver.gecko.driver",
"C:\\selenium\\geckodriver.exe");
driver = new FirefoxDriver();
}
else if (browser.equalsIgnoreCase("IE")) {
System.setProperty("webdriver.ie.driver",
"C:\\selenium\\IEDriverServer.exe");
driver = new InternetExplorerDriver();
}
else if (browser.equalsIgnoreCase("Safari")) {
System.out.println("Safari not supported in Windows. Please
recheck.");
}
else {
throw new Exception("Browser is not correct");
}
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
}
@Test
public void temperature() throws InterruptedException {
try {
driver.get("http://weathershopper.pythonanywhere.com/");
Reporter.log("Browser Invoked and navigated to URL", true);
driver.manage().window().maximize();
mappin.itemsFetch();
//fetching all items available on the menu screen
mappin.itemspriceFetch();
//fetching prices of all items available on the menu screen
mappin.selectButtons();
//fetching the add to cart buttons for all the items
String product_name;
String product_price;
int int_product_price;
int low_price;
Integer.parseInt(mappin.itemspriceFetch().get(k).getText().replaceAll("[^0-
9]", "")),
mappin.itemsFetch().get(k).getText());
}
}
al.add(map_final_products_pro.get(low_price));
al.add(Integer.toString(low_price));
try {
WebDriverWait w = new WebDriverWait(driver, 10);
//wait for the 'Add to Cart' button and 'Checkout' button to be visible on
the screen
w.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("button.thin-
text.nav-link"))).click();
w.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//h2[contains(text(
),'Checkout')]")));
int k = 0;
for (int i = 1; i <= items.length; i++) {
//verification of the elements added to cart VS the items displayed in
checkout screen
for (int j = 1; j <= 2; j++) {
if
(addn.verifyTable().getText().toString().equalsIgnoreCase((String) al.toArray()
[k])) {
try {
w.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("button.stripe
-button-el>span"))).click();
driver.switchTo().frame(0);
//switching to the stripe screen
pay.emailTextFunction().sendKeys(mailid);
//provide the mail id required
Thread.sleep(9000);
stripeScreen(modal);
}
catch (InvalidSelectorException e) {
Reporter.log("catch block - card payment");
e.printStackTrace();
}
}
try {
WebDriverWait w = new WebDriverWait(driver, 10);
w.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//span[@class='Head
er-loggedInEmail']"))).isDisplayed();
check.logoutbtnCheck().click();
//check for logout button
check.emailTextFunction().sendKeys(mailid);
//provide the mail id required
Thread.sleep(4000);
stripeScreen(modal);
}
catch (Exception e) {
check.emailTextFunction().sendKeys(mailid);
//provide the mail id required
Thread.sleep(4000);
stripeScreen(modal);
}
}
try {
if (strip.elementsCheck().size() == 0) {
//check for elements on the stripe screen
Reporter.log("No element found");
}
else {
Thread.sleep(7000);
WebDriverWait w = new WebDriverWait(driver, 10);
//wait for the error message to be visible on the screen
w.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector(".CodeNotRecei
ved-actionMessage"))).isDisplayed();
strip.errorMessageFunction().click();
}
}
catch (NoSuchElementException e) {
e.printStackTrace();
}
finally {
Thread.sleep(2000);
strip.cardNumberFunction().sendKeys("4242424242424242"); //as
per provided on stripe screen
strip.monthYearFunction().sendKeys("09/23");
strip.CVCFunction().sendKeys("363");
strip.ZIPCodeFunction().sendKeys("689121");
strip.rememberMeFunction().click();
strip.telephoneFunction().sendKeys("9876543210");
Thread.sleep(2000);
strip.submitFunction().click();
paymentSuccess(driver);
}
}
driver.switchTo().defaultContent();
Thread.sleep(9000);
if (last.headingFunction().getText() != null) {
//feching text of the last page
if (last.headingFunction().getText().equalsIgnoreCase("Payment
Success")) {
Reporter.log(last.contentFunction().getText());
Reporter.log("Payment successful");
} else {
Reporter.log("Oops! Payment Failed");
}
}
}
}