在Java Selenium中验证表中的Web元素

wfveoks0  于 2022-11-20  发布在  Java
关注(0)|答案(1)|浏览(145)

我正在寻找提取,然后根据某些值的升序或降序进行验证,如图所示,在金额列下。enter image description here从这个网页https://sakshingp.github.io/assignment/home.html在Java selenium 。

package unitTests;

import org.junit.BeforeClass;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

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

public class Wingify {
    private static ChromeDriver driver;
    WebElement p = null;
    private String str;

    void beforeSortAmount() {

    }

    @BeforeClass
    public static void openBrowser() {
        System.setProperty( "webdriver.chrome.driver", "C:\\chromedriver.exe" );
        driver = new ChromeDriver();
        String baseUrl = "https://sakshingp.github.io/assignment/login.html";
        driver.get( baseUrl );
    }

    @Test
    public void MainForm() throws InterruptedException {
        JavascriptExecutor js = (JavascriptExecutor) driver;
//        CountDownLatch waiter = new CountDownLatch(1);
//        waiter.await(3000, TimeUnit.MILLISECONDS); // wait for all elements to load

        driver.manage().window().maximize(); // command to maximize the window
        driver.findElement( By.id( "username" ) ).sendKeys( "Lakshay");
        driver.manage().timeouts().setScriptTimeout( 100, TimeUnit.SECONDS);
        driver.findElement( By.id( "password" ) ).sendKeys( "Wingify");
        driver.manage().timeouts().setScriptTimeout( 100, TimeUnit.SECONDS);
        System.out.println( "Pressing Login button" );
        driver.findElement( By.id( "log-in" ) ).click();
        CountDownLatch waiter = new CountDownLatch(1);
        waiter.await( 3000, TimeUnit.MILLISECONDS );
        driver.manage().timeouts().setScriptTimeout( 2000, TimeUnit.SECONDS);
        js.executeScript( "window.scrollBy(0,120)" ); // scrolling down to confirm message sent
        System.out.println( "Going to list all values" );
        System.out.println("Now clicking Amount button to sort");
        driver.findElement( By.id( "amount" ) ).click();
        List<WebElement> beforeSortedAmount = driver.findElementsByXPath("//*[@id=\"transactionsTable\"]/tbody/tr/td/span");
        String[] beforeSortAmountList = new String[beforeSortedAmount.size()];
        for (int i = 0; i < beforeSortedAmount.size(); i++) {
        beforeSortAmountList[i] = (beforeSortedAmount.get( i ).getText().trim().replace( "USD", ""));
        }
        System.out.println("Before Sort Amount");
        Print(beforeSortAmountList);
        Arrays.sort( beforeSortAmountList );
        System.out.println("After Sort");
        Print( beforeSortAmountList );

        driver.manage().timeouts().setScriptTimeout( 30, TimeUnit.SECONDS);
        Print( beforeSortAmountList );

    }

    private void Print(String[] beforeSortAmountList) {
        for(int i = 0;i<beforeSortAmountList.length;i++){
         System.out.println(beforeSortAmountList[i]);
        }
    }
}

//        Collections.sort( beforeSortedAmountList );
//        Assert.assertEquals( "beforeSortedAmountList" , "afterSortedAmountList");

//    @AfterClass
//    public static void closeBrowser(){
//        driver.quit(); // closing browser
//    }

我希望输出为:

  • 1,250.00
  • 17.99
  • 340.00
  • 952.23
  • 244.00
  • 320.00,仅按递增或递减顺序。
nfzehxib

nfzehxib1#

List<WebElement> beforeSortedAmount = driver.findElementsByXPath("//*[@id=\"transactionsTable\"]/tbody/tr/td/span");
List<String> actualData = new ArrayList<String>();
for(WebElement each: beforeSortedAmount)
{
    data.Add(each.getText().replaceAll("USD", "").replaceAll(",", "").replaceAll(" " , "").trim();
}

List<String> expectedData = actualData;
collections.sort(expectedData)
Assert.AssertEquals(expectedData, actualData);

相关问题