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

Assignment 4

Advance data visualization
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views

Assignment 4

Advance data visualization
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

SE 5910 Advance Software Engineering

University of Central Missouri, Summer-1, 2024


CRN : 30786 & Student ID : 700766559

Assignment 4
Veera Venkata Sai Kashyap Kaligotla

1 Selenium Setup
Setting up Selenium for our project involves several key steps. Firstly, we need to down-
load the Selenium Client & WebDriver Language Bindings for Java from the official
Selenium website. After extracting the downloaded ZIP file, we add the JAR files to our
Java project. We then download the appropriate WebDriver for our browser of choice,
such as ChromeDriver for Google Chrome, from the download pages. Once downloaded,
we place the WebDriver executable in a convenient location on our system. Next, we con-
figure our development environment, such as Eclipse IDE, by creating a new Java project
and adding the Selenium JAR files to the project’s build path. We set the WebDriver ex-
ecutable path in our code using the ‘System.setProperty‘ method. This configuration
ensures that our Selenium scripts can interact with the browser correctly. With these
steps completed, we are ready to write and execute our Selenium scripts to automate
browser tasks and perform web testing. This setup process lays the foundation for the
successful execution of the automated tasks outlined in the assignment.

2 Tasks:
This assignment involved the creation and execution of four specific Selenium tasks to
automate and verify various functionalities on the Amazon website.

2.1 Verify the Correct Price Display:


The first task focused on verifying the correct price display of a product added to the
shopping cart. This involved searching for a product, adding it to the cart, navigating to
the cart page, and capturing the displayed price.
// Necessary imports
import org . openqa . selenium . By ;
import org . openqa . selenium . WebDriver ;
import org . openqa . selenium . WebElement ;
import org . openqa . selenium . chrome . ChromeDriver ;
import java . io . File ;
import org . openqa . selenium . OutputType ;
import org . openqa . selenium . TakesScreenshot ;
import org . apache . commons . io . FileUtils ;

public class VerifyPrice {


SE 5910 Assignment 4 Veera Venkata Sai Kashyap Kaligotla

public static void main ( String [] args ) throws Exception {


System . setProperty ( " webdriver . chrome . driver " , " path / to /
chromedriver " ) ;

WebDriver driver = new ChromeDriver () ;


driver . get ( " https :// www . amazon . com " ) ;

// Search for a product


WebElement searchBox = driver . findElement ( By . id ( "
twotabsearchtextbox " ) ) ;
searchBox . sendKeys ( " laptop " ) ;
searchBox . submit () ;

// Click on the first product


WebElement firstProduct = driver . findElement ( By .
cssSelector ( " span .a - size - medium " ) ) ;
firstProduct . click () ;

// Add product to cart


WebElement addToCartButton = driver . findElement ( By . id ( " add
- to - cart - button " ) ) ;
addToCartButton . click () ;

// Go to cart
WebElement cart = driver . findElement ( By . id ( " nav - cart " ) ) ;
cart . click () ;

// Verify the price


WebElement priceElement = driver . findElement ( By .
cssSelector ( " . sc - price " ) ) ;
System . out . println ( " Price : " + priceElement . getText () ) ;

// Take a screenshot
File screenshot = (( TakesScreenshot ) driver ) .
getScreenshotAs ( OutputType . FILE ) ;
FileUtils . copyFile ( screenshot , new File ( " price_display . png
"));

driver . quit () ;
}
}

2.1.1 Apply Coupon Codes:


The second task was to check the availability of coupon code application functionality on
the cart page. This required adding a product to the cart and verifying if the option to
enter a coupon code was present.
import org . openqa . selenium . By ;
import org . openqa . selenium . WebDriver ;
import org . openqa . selenium . WebElement ;

2
SE 5910 Assignment 4 Veera Venkata Sai Kashyap Kaligotla

Figure 1: Screenshot of the Output after I used Selenium to Verify the Correct Price

import org . openqa . selenium . chrome . ChromeDriver ;


import java . io . File ;
import org . openqa . selenium . OutputType ;
import org . openqa . selenium . TakesScreenshot ;
import org . apache . commons . io . FileUtils ;

public class ApplyCoupon {


public static void main ( String [] args ) throws Exception {
System . setProperty ( " webdriver . chrome . driver " , " path / to /
chromedriver " ) ;

WebDriver driver = new ChromeDriver () ;


driver . get ( " https :// www . amazon . com " ) ;

// Add a product to the cart first as shown in the


previous script

// Check for coupon code option


WebElement couponCode = driver . findElement ( By . cssSelector (
" input [ name = ’ promoCode ’] " ) ) ;
if ( couponCode != null ) {
System . out . println ( " Coupon code option is available . " )
;
}

// Take a screenshot
File screenshot = (( TakesScreenshot ) driver ) .
getScreenshotAs ( OutputType . FILE ) ;
FileUtils . copyFile ( screenshot , new File ( "
coupon_code_option . png " ) ) ;

driver . quit () ;

3
SE 5910 Assignment 4 Veera Venkata Sai Kashyap Kaligotla

}
}

Figure 2: Screenshot of the Output after I used Selenium to Apply Coupun code task

2.2 Adjust Quantity:


The third task dealt with adjusting the quantity of a product in the cart. We automated
the process of increasing and then decreasing the product quantity, ensuring that the
changes were correctly reflected.
import org . openqa . selenium . By ;
import org . openqa . selenium . WebDriver ;
import org . openqa . selenium . WebElement ;
import org . openqa . selenium . chrome . ChromeDriver ;
import java . io . File ;
import org . openqa . selenium . OutputType ;
import org . openqa . selenium . TakesScreenshot ;
import org . apache . commons . io . FileUtils ;

public class AdjustQuantity {


public static void main ( String [] args ) throws Exception {
System . setProperty ( " webdriver . chrome . driver " , " path / to /
chromedriver " ) ;

WebDriver driver = new ChromeDriver () ;


driver . get ( " https :// www . amazon . com " ) ;

// Add a product to the cart first as shown in the


previous script

// Increase quantity
WebElement quantityDropdown = driver . findElement ( By . name ( "
quantity " ) ) ;

4
SE 5910 Assignment 4 Veera Venkata Sai Kashyap Kaligotla

quantityDropdown . sendKeys ( " 2 " ) ;

// Take a screenshot
File screenshot1 = (( TakesScreenshot ) driver ) .
getScreenshotAs ( OutputType . FILE ) ;
FileUtils . copyFile ( screenshot1 , new File ( "
quantity_increased . png " ) ) ;

// Decrease quantity
quantityDropdown . sendKeys ( " 1 " ) ;

// Take a screenshot
File screenshot2 = (( TakesScreenshot ) driver ) .
getScreenshotAs ( OutputType . FILE ) ;
FileUtils . copyFile ( screenshot2 , new File ( "
quantity_decreased . png " ) ) ;

driver . quit () ;
}
}

Figure 3: Screenshot of the Output after I used Selenium to Adjust quantity code task

2.3 Remove Product:


Finally, the fourth task was to remove a product from the cart. This involved automating
the removal process and confirming that the product was successfully deleted from the
cart.
import org . openqa . selenium . By ;
import org . openqa . selenium . WebDriver ;
import org . openqa . selenium . WebElement ;
import org . openqa . selenium . chrome . ChromeDriver ;
import java . io . File ;

5
SE 5910 Assignment 4 Veera Venkata Sai Kashyap Kaligotla

import org . openqa . selenium . OutputType ;


import org . openqa . selenium . TakesScreenshot ;
import org . apache . commons . io . FileUtils ;

public class RemoveProduct {


public static void main ( String [] args ) throws Exception {
System . setProperty ( " webdriver . chrome . driver " , " path / to /
chromedriver " ) ;

WebDriver driver = new ChromeDriver () ;


driver . get ( " https :// www . amazon . com " ) ;

// Add a product to the cart first as shown in the


previous script

// Remove product
WebElement deleteButton = driver . findElement ( By . name ( "
submit . delete . CART_ITEM_ID " ) ) ;
deleteButton . click () ;

// Take a screenshot
File screenshot = (( TakesScreenshot ) driver ) .
getScreenshotAs ( OutputType . FILE ) ;
FileUtils . copyFile ( screenshot , new File ( " product_removed .
png " ) ) ;

driver . quit () ;
}
}

Figure 4: Screenshot of the Output after I used Selenium to Remove product task

You might also like