验证Java selenium中表中的web元素



我希望根据某些值的增加或减少顺序提取然后验证,如此图像中Amount列下所示。在这里输入图像描述从这个网页https://sakshingp.github.io/assignment/home.html

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
//    }

I want out put as:

  • 1250
  • 17.99
  • 340.00
  • 952.23
  • 244.00
  • 320.00
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);

最新更新