UI 测试 - 断言排序数据 (Katalon/Selenium) Java
UI Testing - Asserting on Sorting Data (Katalon/Selenium) Java
我正准备围绕 table 排序编写一些自动化 UI 测试。
排序允许 table 中的所有列按升序和降序排列。列可以是字符串、日期时间、整数等。
任何人都可以建议一种实用的方法来断言列已按预期排序吗?
是否有一种方法不依赖于每次测试的数据相同?
我想出了以下使用 selenium 和 Groovy (Java) 的解决方案。使用这种脚本语言的原因是因为我的组织使用了一个名为 Katalon 的工具,它支持上面的自定义脚本。
先决条件
- 将 ID 添加到我要断言的列的 table 上的每个单元格。这使得选择元素更容易。例如,
<td id="receivedDate-1">2019-04-02 00:00</td>
等等。
这是我的解决方案
import org.openqa.selenium.By as By
import org.openqa.selenium.WebDriver as WebDriver
import org.openqa.selenium.WebElement as WebElement
import com.kms.katalon.core.webui.driver.DriverFactory as DriverFactory
import com.kms.katalon.core.util.KeywordUtil as Log
WebDriver driver = DriverFactory.getWebDriver()
int untracedPostTableRowCount = driver.findElements(By.xpath('//table[@id=\'UntracedPostTable\']/tbody/tr')).size()
Log.logInfo('untracedPostTableRowCount: ' + untracedPostTableRowCount)
// Start iteration from second row to compare against the first row
for (int rowIndex = 2; rowIndex <= untracedPostTableRowCount; rowIndex++) {
//Get the UTC date from within the hidden span element
String currentRowReceivedDate = driver.findElement(By.id("receivedDate-$rowIndex-utc")).getAttribute('innerHTML');
int previousRowIndex = rowIndex - 1;
String previousRowReceivedDate = driver.findElement(By.id("receivedDate-$previousRowIndex-utc")).getAttribute('innerHTML');
// compareTo() - https://beginnersbook.com/2013/12/java-string-compareto-method-example/
// compareTo method converts strings into a Unicode value for comparison
// If strings are equal, compareTo will return 0
// if first string is lexicographically greater than the second string, compareTo will return a positive int
// if first string is lexicographically small than the first string, compareTo will return a negative int
assert currentRowReceivedDate.compareTo(previousRowReceivedDate) >= 0;
}
参考:https://forum.katalon.com/t/testing-data-that-has-been-sorted/36785
这是我的看法:
- 转到https://the-internet.herokuapp.com/tables页
- 单击第一个 table 上的 "Last Name" header 以便对 table
进行排序
- 看是否通过比较UI值和
Collections.sort()
方法排序
WebUI.openBrowser("https://the-internet.herokuapp.com/tables")
WebDriver driver = DriverFactory.getWebDriver()
driver.findElement(By.xpath("//table[@id='table1']//span[contains(.,'Last Name')]")).click()
List<WebElement> tableElements = driver.findElements(By.cssSelector("#table1 tr td:nth-child(1)"));
ArrayList<String> tableValues = new ArrayList<String>();
for(int i=0; i < tableElements.size(); i++){
String str = tableElements.get(i).getText();
tableValues.add(str);
}
ArrayList<String> referenceValues = new ArrayList<String>();
for(int i=0; i < tableValues.size(); i++){
referenceValues.add(tableValues.get(i))
}
Collections.sort(referenceValues)
assert referenceValues.equals(tableValues)
我正准备围绕 table 排序编写一些自动化 UI 测试。
排序允许 table 中的所有列按升序和降序排列。列可以是字符串、日期时间、整数等。
任何人都可以建议一种实用的方法来断言列已按预期排序吗?
是否有一种方法不依赖于每次测试的数据相同?
我想出了以下使用 selenium 和 Groovy (Java) 的解决方案。使用这种脚本语言的原因是因为我的组织使用了一个名为 Katalon 的工具,它支持上面的自定义脚本。
先决条件
- 将 ID 添加到我要断言的列的 table 上的每个单元格。这使得选择元素更容易。例如,
<td id="receivedDate-1">2019-04-02 00:00</td>
等等。
这是我的解决方案
import org.openqa.selenium.By as By
import org.openqa.selenium.WebDriver as WebDriver
import org.openqa.selenium.WebElement as WebElement
import com.kms.katalon.core.webui.driver.DriverFactory as DriverFactory
import com.kms.katalon.core.util.KeywordUtil as Log
WebDriver driver = DriverFactory.getWebDriver()
int untracedPostTableRowCount = driver.findElements(By.xpath('//table[@id=\'UntracedPostTable\']/tbody/tr')).size()
Log.logInfo('untracedPostTableRowCount: ' + untracedPostTableRowCount)
// Start iteration from second row to compare against the first row
for (int rowIndex = 2; rowIndex <= untracedPostTableRowCount; rowIndex++) {
//Get the UTC date from within the hidden span element
String currentRowReceivedDate = driver.findElement(By.id("receivedDate-$rowIndex-utc")).getAttribute('innerHTML');
int previousRowIndex = rowIndex - 1;
String previousRowReceivedDate = driver.findElement(By.id("receivedDate-$previousRowIndex-utc")).getAttribute('innerHTML');
// compareTo() - https://beginnersbook.com/2013/12/java-string-compareto-method-example/
// compareTo method converts strings into a Unicode value for comparison
// If strings are equal, compareTo will return 0
// if first string is lexicographically greater than the second string, compareTo will return a positive int
// if first string is lexicographically small than the first string, compareTo will return a negative int
assert currentRowReceivedDate.compareTo(previousRowReceivedDate) >= 0;
}
参考:https://forum.katalon.com/t/testing-data-that-has-been-sorted/36785
这是我的看法:
- 转到https://the-internet.herokuapp.com/tables页
- 单击第一个 table 上的 "Last Name" header 以便对 table 进行排序
- 看是否通过比较UI值和
Collections.sort()
方法排序
WebUI.openBrowser("https://the-internet.herokuapp.com/tables")
WebDriver driver = DriverFactory.getWebDriver()
driver.findElement(By.xpath("//table[@id='table1']//span[contains(.,'Last Name')]")).click()
List<WebElement> tableElements = driver.findElements(By.cssSelector("#table1 tr td:nth-child(1)"));
ArrayList<String> tableValues = new ArrayList<String>();
for(int i=0; i < tableElements.size(); i++){
String str = tableElements.get(i).getText();
tableValues.add(str);
}
ArrayList<String> referenceValues = new ArrayList<String>();
for(int i=0; i < tableValues.size(); i++){
referenceValues.add(tableValues.get(i))
}
Collections.sort(referenceValues)
assert referenceValues.equals(tableValues)