Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
9.6 kB
3
Indexable
Never
package Topic08;

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

import java.time.Duration;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;
import java.util.concurrent.TimeUnit;

public class Topic08_B01_Homework {
    WebDriver driver;
    WebDriverWait wait;

    @BeforeTest
    public void setUpBeforeExecuteTC() {
        driver = new ChromeDriver();
        driver.manage().window().maximize();
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        wait = new WebDriverWait(driver, Duration.ofSeconds(30));
    }
    @Test
    public void hw01() {
        driver.get("https://www.saucedemo.com/");
        Assert.assertEquals(driver.getCurrentUrl(),"https://www.saucedemo.com/");
        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[@type='submit']")).click();
        Assert.assertEquals(driver.findElement(By.xpath("//div[@class='app_logo']")).getText(),"Swag Labs");
        List<WebElement> itemPrice = driver.findElements(By.xpath("//div[@class='inventory_item']//div[@class='inventory_item_price']"));
        List<Float> prices = new ArrayList<>();
        for (WebElement item: itemPrice)
        {
             prices.add(Float.parseFloat(item.getText().replace("$","")));
        }
        //assert cái gì vào đây ạ
        Collections.sort(prices);
        driver.findElement(By.xpath("//div[@class='inventory_item_price' and text()='"+prices.get(0).toString()+"']//following::button[1]")).click();
        driver.findElement(By.xpath("//div[@class='inventory_item_price' and text()='"+prices.get(1).toString()+"']//following::button[1]")).click();
        Assert.assertEquals(driver.findElement(By.xpath("//span[@class='shopping_cart_badge']")).getText(),"2");
        driver.findElement(By.xpath("//a[@class='shopping_cart_link']")).click();
        Assert.assertEquals(driver.findElement(By.xpath("//span[@class='title']")).getText(),"Your Cart");
        driver.findElement(By.xpath("//button[@id='checkout']")).click();
        Assert.assertEquals(driver.findElement(By.xpath("//span[@class='title']")).getText(),"Checkout: Your Information");
        driver.findElement(By.xpath("//input[@id='first-name']")).sendKeys("Uyen");
        driver.findElement(By.xpath("//input[@id='last-name']")).sendKeys("Nguyen");
        driver.findElement(By.xpath("//input[@id='postal-code']")).sendKeys("10000");
        driver.findElement(By.xpath("//input[@id='continue']")).click();
        Assert.assertEquals(driver.findElement(By.xpath("//span[@class='title']")).getText(),"Checkout: Overview");
        JavascriptExecutor jsExecutor = (JavascriptExecutor) driver;
        jsExecutor.executeScript("arguments[0].scrollIntoView(true);",driver.findElement(By.xpath("//button[@id='finish']")));
        driver.findElement(By.xpath("//button[@id='finish']")).click();
        Assert.assertEquals(driver.findElement(By.xpath("//span[@class='title']")).getText(),"Checkout: Complete!");
        driver.findElement(By.xpath("//button[@id='back-to-products']")).click();
        Assert.assertEquals(driver.getCurrentUrl(),"https://www.saucedemo.com/inventory.html");
    }
    @Test
    public void hw02() {
        List<String> memberNames = new ArrayList<>();
        memberNames.add("Nguyen Thi Thao Uyen");
        memberNames.add("Nguyen Huu Huan");
        memberNames.add("Bui Huong Giang");
        memberNames.add("Ba Thi Thom");
        memberNames.add("Do Thi Hau");
        memberNames.add("Ngo Anh Thai");
        memberNames.add("Nguyen Thi Chung");
        memberNames.add("Nguyen Thi Huong Linh");
        memberNames.add("Nguyen Thi Mai Dung");
        memberNames.add("Nguyen Thuy Tien");
        List<String> emails = new ArrayList<>();
        for (String member: memberNames)
        {
            String[] split = member.split(" ");
            String email = split[split.length-1];
            for (int i=0;i<=split.length-2;i++){
                email+=split[i].charAt(0);
            }
            email+="@vmogroup.com";
            emails.add(email);
        }
    }
    @Test
    public void hw3(){
        Random random = new Random();
        int temperature = random.nextInt(41) - 10;
        System.out.println(temperature);
        String result1, result2;
        if (temperature < 0) {
            result1 = "Freezing";
        } else if (temperature >= 0 && temperature <= 10) {
            result1 = "Cold";
        } else if (temperature >= 11 && temperature <= 20) {
            result1 = "Moderate";
        } else if (temperature >= 21 && temperature <= 30) {
            result1 = "Warm";
        } else {
            result1 = "Hot";
        }

        switch (temperature) {
            case -10:
                result2 = "Freezing";
                break;
            case -9:
                result2 = "Freezing";
                break;
            case -8:
                result2 = "Freezing";
                break;
            case -7:
                result2 = "Freezing";
                break;
            case -6:
                result2 = "Freezing";
                break;
            case -5:
                result2 = "Freezing";
                break;
            case -4:
                result2 = "Freezing";
                break;
            case -3:
                result2 = "Freezing";
                break;
            case -2:
                result2 = "Freezing";
                break;
            case -1:
                result2 = "Freezing";
                break;
            case 0:
                result2 = "Cold";
                break;
            case 1:
                result2 = "Cold";
                break;
            case 2:
                result2 = "Cold";
                break;
            case 3:
                result2 = "Cold";
                break;
            case 4:
                result2 = "Cold";
                break;
            case 5:
                result2 = "Cold";
                break;
            case 6:
                result2 = "Cold";
                break;
            case 7:
                result2 = "Cold";
                break;
            case 8:
                result2 = "Cold";
                break;
            case 9:
                result2 = "Cold";
                break;
            case 10:
                result2 = "Cold";
                break;
            case 11:
                result2 = "Moderate";
                break;
            case 12:
                result2 = "Moderate";
                break;
            case 13:
                result2 = "Moderate";
                break;
            case 14:
                result2 = "Moderate";
                break;
            case 15:
                result2 = "Moderate";
                break;
            case 16:
                result2 = "Moderate";
                break;
            case 17:
                result2 = "Moderate";
                break;
            case 18:
                result2 = "Moderate";
                break;
            case 19:
                result2 = "Moderate";
                break;
            case 20:
                result2 = "Moderate";
                break;
            case 21:
                result2 = "Warm";
                break;
            case 22:
                result2 = "Warm";
                break;
            case 23:
                result2 = "Warm";
                break;
            case 24:
                result2 = "Warm";
                break;
            case 25:
                result2 = "Warm";
                break;
            case 26:
                result2 = "Warm";
                break;
            case 27:
                result2 = "Warm";
                break;
            case 28:
                result2 = "Warm";
                break;
            case 29:
                result2 = "Warm";
                break;
            case 30:
                result2 = "Warm";
                break;
            default:
                result2 = "Hot";
                break;
        }
        System.out.println("result using If-else = "+result1);
        System.out.println("result using Switch case = "+result2);
    }
    public void sendKeys(WebElement element, String text) {
        element.sendKeys(text);
    }
    public void sendKeys(WebElement element,boolean clickBefore ,String text) {
        element.click();
        element.sendKeys(text);
    }
    public static void sendKeys(WebElement element, boolean clickBefore, boolean clearBefore, String text) {
        if (clickBefore) {
            element.click();
        }
        if (clearBefore) {
            element.clear();
        }
        element.sendKeys(text);
    }

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