Untitled

mail@pastecode.io avatar
unknown
plain_text
7 months ago
2.7 kB
0
Indexable
Never
package MiniProject;

import java.time.Duration;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.support.ui.WebDriverWait;

import io.github.bonigarcia.wdm.WebDriverManager;

public class FlipkartAutomation {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		//Launching the Chrome Browser
		WebDriverManager.chromedriver().setup();
		ChromeDriver driver=new ChromeDriver();
		
	    driver.get("https://www.flipkart.com/");
	    WebElement searchBox=driver.findElement(By.name("q"));
	    //Search for mobiles under 15000
	    searchBox.sendKeys("mobiles");
	    WebDriverWait wait=new WebDriverWait(driver,Duration.ofSeconds(10));
	    wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("/html/body/div[3]/div/span"))).click();
	    
	    
	    
	    //Apply price filter and select OS version
	    WebElement priceFilter = driver.findElement(By.xpath("//div[text()='Operating System']//following-sibling::div//input"));
	    WebElement osVersion = driver.findElement(By.xpath("//div[text()='Operating System']//following-sibling::div//input"));
        osVersion.sendKeys("Pie");
        
        //Sort by newest first
        WebElement sortDropdown=driver.findElement(By.className("Z3mXg"));
        Select sortBy=new Select(sortDropdown);
        sortBy.selectByVisibleText("Newest First");
        
        //Display first five mobiles' Name and Price
        for(int i=1;i<=5;i++) {
        	WebElement mobileName = driver.findElement(By.xpath("(//div[@class='_4rR01T'])[" + i + "]"));
            WebElement mobilePrice = driver.findElement(By.xpath("(//div[@class='_30jeq3 _1_WHN1'])[" + i + "]"));
            System.out.println("Mobile " + i + " - Name: " + mobileName.getText() + ", Price: " + mobilePrice.getText());
        }
        
        
        //Validate first mobile's price<30000
        WebElement firstMobilePrice = driver.findElement(By.xpath("(//div[@class='_30jeq3 _1_WHN1'])[1]"));
        String priceString = firstMobilePrice.getText().replaceAll("[^0-9]", ""); // Remove non-numeric characters
        int price = Integer.parseInt(priceString);
        if (price < 30000) {
            System.out.println("First mobile price is less than Rs. 30000");
        } else {
            System.out.println("First mobile price is not less than Rs. 30000");
        }
        
        // Close the browser
        driver.quit();
	}
}
Leave a Comment