Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
2.9 kB
1
Indexable
Never
package com.material.qa;


import io.github.bonigarcia.wdm.WebDriverManager;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

import java.util.List;
import java.util.concurrent.TimeUnit;

public class RunTest {
    WebDriver driver;
    @BeforeClass
    public void beforeClass() {
        WebDriverManager.chromedriver().setup();
        driver = new ChromeDriver();
        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
        driver.manage().window().maximize();

        driver.get("https://material.angular.io/components/table/overview");
    }

    public void Test01_Get_Weight_Based_On_Name() {

//        WebElement overviewTable = driver.findElement(By.xpath("//example-viewer[@id='table-basic']//table[contains(@class,'mat-mdc-table')]"));
//        sleepInSecond(3);
        List<WebElement> rows = driver.findElements(By.xpath("//example-viewer[@id='table-basic']//table[contains(@class,'mat-mdc-table')]//tbody[@class='mdc-data-table__content']//tr"));


        String desiredName = "Hydrogen";
        String weightBasedOnName = "";
        sleepInSecond(3);
        for (WebElement row : rows) {
            WebElement cellName = row.findElement(By.xpath
                    ("//example-viewer[@id='table-basic']//table[contains(@class,'mat-mdc-table')]//tbody[@class='mdc-data-table__content']//tr//td[contains(@class,'cdk-column-name')]"));

            String name = cellName.getText().trim();
            System.out.println(name);

             if (name.equals(desiredName)){
                 WebElement cellWeight = row.findElement(By.xpath
                         ("//example-viewer[@id='table-basic']//table[contains(@class,'mat-mdc-table')]//tbody[@class='mdc-data-table__content']//tr//td[contains(@class,'cdk-column-weigh')]"));
                 weightBasedOnName = cellWeight.getText().trim();
                 System.out.println(weightBasedOnName);
                 break;
             }
        }
        sleepInSecond(5);
        System.out.println("name: " +desiredName);
        System.out.println("weigh: " + weightBasedOnName);
    }
    @Test
    public void TC02(){
        driver.findElement(By.xpath("//span[text()='api']//ancestor::a[@id='mat-tab-link-2']")).click();
        driver.findElement(By.xpath("//h3[@id='material-table-testing-interfaces']")).click();
        sleepInSecond(5);
    }

    @AfterClass
    public void afterClass() {
        driver.quit();
    }
    public void sleepInSecond(long second) {
        try {
            Thread.sleep(second * 1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}