0% found this document useful (0 votes)
5 views

The Real Life Selenium Scenarios

The document is a Java Selenium test script for automating interactions with the Sauce Labs demo website. It includes setup and teardown methods for the WebDriver, and two test methods that add the highest and lowest priced products to the cart while printing their names and prices. The script also contains a login function to authenticate the user before performing the tests.

Uploaded by

jadhavbs16
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

The Real Life Selenium Scenarios

The document is a Java Selenium test script for automating interactions with the Sauce Labs demo website. It includes setup and teardown methods for the WebDriver, and two test methods that add the highest and lowest priced products to the cart while printing their names and prices. The script also contains a login function to authenticate the user before performing the tests.

Uploaded by

jadhavbs16
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

package com.

practice;

import java.time.Duration;
import java.util.List;

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.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class SauceLabsDemo {

public WebDriver driver;


public JavascriptExecutor je;

@BeforeTest
public void setup()
{

driver = new ChromeDriver();


driver.manage().window().maximize();

driver.manage().timeouts().pageLoadTimeout(Duration.ofSeconds(15));
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(20));

driver.get("https://www.saucedemo.com/"); // Username & password are


mentioned in website.

@AfterTest
public void teardown()
{
driver.quit();
}

@Test(priority=1)
public void VerifyAddtoProductHigh() throws InterruptedException //To add
product of highest value to the cart & printing its name & description
{
login(); //Calling function login

List <WebElement> pricelist =


driver.findElements(By.className("inventory_item_price"));

Double max_value_price = pricelist


.stream()
.mapToDouble(e ->
Double.parseDouble(e.getText().trim().replace("$","")))
.max()
.getAsDouble();

String xpath_name = "//div[normalize-


space()='$"+max_value_price+"']//..//..//div[@class=\"inventory_item_label\"]";
String max_product_name =
driver.findElement(By.xpath(xpath_name)).getText();

//Printing name & price of product

System.out.println(max_product_name);
System.out.println(max_value_price);

//Adding a product to cart having lowest value

String xpath_addtocart = "//div[normalize-space()=


'$"+max_value_price+"']/following-sibling::button[text()='Add to cart']";
driver.findElement(By.xpath(xpath_addtocart)).click();

Thread.sleep(3000);

@Test(priority=2)
public void VerifyAddtoProductLow() throws InterruptedException //To add
product of lowest value to the cart & printing its name & description
{

List <WebElement> pricelist =


driver.findElements(By.className("inventory_item_price"));

Double min_value_price = pricelist


.stream()
.mapToDouble(e ->
Double.parseDouble(e.getText().trim().replace("$","")))
.min()
.getAsDouble();

String xpath_name = "//div[normalize-


space()='$"+min_value_price+"']//..//..//div[@class=\"inventory_item_label\"]";
String min_product_name =
driver.findElement(By.xpath(xpath_name)).getText();
//Printing name & price of product

System.out.println(min_product_name);
System.out.println(min_value_price);

//Adding a product to cart having lowest value

String xpath_addtocart = "//div[normalize-space()=


'$"+min_value_price+"']/following-sibling::button[text()='Add to cart']";
driver.findElement(By.xpath(xpath_addtocart)).click();

Thread.sleep(3000); //Just to see action you can remove it - not needed

private void login() //Login function for user login


{
driver.findElement(By.xpath("//input[@id='user-
name']")).sendKeys("standard_user");

driver.findElement(By.xpath("//input[@id='password']")).sendKeys("secret_sauce
");
driver.findElement(By.xpath("//input[@id='login-button']")).click();
}
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy