使用 Selenium Java 我想自动化一个包含价目表和价格计算器的 table
Using Selenium Java I want to automate a table that contains a price list and a price calculator
问题: 使用 Selenium Java 我想自动化一个包含价目表和价格计算器的 table
另一个小问题:我不能点击“分析”(过滤)按钮,因为我必须双击,在这种情况下我可以这样做
我想要的是:我想找个方法让我运行程序的时候,程序会点击另一个分析(在另一个+按钮)现在借助选择器,我只能单击第一个 (+)。但我希望每次测试程序时,随机进行其他分析(其他按钮+)。
我做什么
@FindBy(xpath = "//*[@id='footable_501']/thead/tr[2]/th[1]")
WebElement analizaSort;
@FindBy(xpath = "//*[@id='footable_501']/thead/tr[2]/th[2]")
WebElement pretSort;
@FindBy(xpath = "//*[@id='calculator']/div[1]/div[2]/div[2]")
WebElement total;
public void checkCalculator()
{
add.click();
add2.click();
}
public void checkFilter()
{
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("window.scrollBy(0,500)"); //Scroll vertically down by 1000 pixels
analizaSort.click();
analizaSort.click();
pretSort.click();
}
我在代码下方添加了点击随机分析并计算总数的代码:
@FindBy(css = "th.footable-sortable[class*='pret']") // css locator is faster than xpath
WebElement pretSort;
@FindBy(css = "th.footable-sortable[class*='analiza']")
WebElement analizaSort;
@FindBy(css = "tr[class*='row'] td[class*='pret']")
List<WebElement> analyzePriceList;
@FindBy(css = "#calculator .total .right")
WebElement total;
public void checkCalculator() {
int elementListSize = analyzePriceList.size();
assertTrue("No analyze was found in the table", elementListSize != 0); // replace here with the specific of your testing framework
int elementIndex = getRandomNumber(elementListSize - 1);
scrollElementToTheMiddle(analyzePriceList.get(elementIndex));
int expectedTotal = getTextAsInt(analyzePriceList.get(elementIndex));
analyzePriceList.get(elementIndex).click();
String totalAsString = total.getText().replace("lei", "");
int actualTotal = getInt(totalAsString);
assertEquals(expectedTotal, actualTotal);
}
public void checkFilter() {
scrollElementToTheMiddle(analizaSort);
analizaSort.click();
analizaSort.click(); // if you need double click, please see the below method
pretSort.click();
}
private void doubleClick(WebElement element) {
Actions act = new Actions(driver);
act.doubleClick(element).build().perform();
}
private int getTextAsInt(WebElement element) {
String text = element.getText();
return getInt(text);
}
private int getInt(String text) {
assertTrue("The " + text + " text was expected to be numeric", isNumeric(text)); // replace here with the specific of your testing framework
return Integer.parseInt(text);
}
private boolean isNumeric(String possibleNumberAsString) {
Pattern pattern = Pattern.compile("-?\d+(\.\d+)?\n");
if (possibleNumberAsString == null) {
return false;
}
return pattern.matcher(possibleNumberAsString.trim()).matches();
}
private int getRandomNumber(int maximum) {
return ThreadLocalRandom.current().nextInt(0, maximum);
}
private void scrollElementToTheMiddle(WebElement element) {
String scrollElementIntoMiddle = "var viewPortHeight = Math.max(document.documentElement.clientHeight, window.innerHeight || 0);"
+ "var elementTop = arguments[0].getBoundingClientRect().top;"
+ "window.scrollBy(0, elementTop-(viewPortHeight/2));";
((JavascriptExecutor) driver).executeScript(scrollElementIntoMiddle, element);
}
问题: 使用 Selenium Java 我想自动化一个包含价目表和价格计算器的 table
另一个小问题:我不能点击“分析”(过滤)按钮,因为我必须双击,在这种情况下我可以这样做
我想要的是:我想找个方法让我运行程序的时候,程序会点击另一个分析(在另一个+按钮)现在借助选择器,我只能单击第一个 (+)。但我希望每次测试程序时,随机进行其他分析(其他按钮+)。
我做什么
@FindBy(xpath = "//*[@id='footable_501']/thead/tr[2]/th[1]")
WebElement analizaSort;
@FindBy(xpath = "//*[@id='footable_501']/thead/tr[2]/th[2]")
WebElement pretSort;
@FindBy(xpath = "//*[@id='calculator']/div[1]/div[2]/div[2]")
WebElement total;
public void checkCalculator()
{
add.click();
add2.click();
}
public void checkFilter()
{
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("window.scrollBy(0,500)"); //Scroll vertically down by 1000 pixels
analizaSort.click();
analizaSort.click();
pretSort.click();
}
我在代码下方添加了点击随机分析并计算总数的代码:
@FindBy(css = "th.footable-sortable[class*='pret']") // css locator is faster than xpath
WebElement pretSort;
@FindBy(css = "th.footable-sortable[class*='analiza']")
WebElement analizaSort;
@FindBy(css = "tr[class*='row'] td[class*='pret']")
List<WebElement> analyzePriceList;
@FindBy(css = "#calculator .total .right")
WebElement total;
public void checkCalculator() {
int elementListSize = analyzePriceList.size();
assertTrue("No analyze was found in the table", elementListSize != 0); // replace here with the specific of your testing framework
int elementIndex = getRandomNumber(elementListSize - 1);
scrollElementToTheMiddle(analyzePriceList.get(elementIndex));
int expectedTotal = getTextAsInt(analyzePriceList.get(elementIndex));
analyzePriceList.get(elementIndex).click();
String totalAsString = total.getText().replace("lei", "");
int actualTotal = getInt(totalAsString);
assertEquals(expectedTotal, actualTotal);
}
public void checkFilter() {
scrollElementToTheMiddle(analizaSort);
analizaSort.click();
analizaSort.click(); // if you need double click, please see the below method
pretSort.click();
}
private void doubleClick(WebElement element) {
Actions act = new Actions(driver);
act.doubleClick(element).build().perform();
}
private int getTextAsInt(WebElement element) {
String text = element.getText();
return getInt(text);
}
private int getInt(String text) {
assertTrue("The " + text + " text was expected to be numeric", isNumeric(text)); // replace here with the specific of your testing framework
return Integer.parseInt(text);
}
private boolean isNumeric(String possibleNumberAsString) {
Pattern pattern = Pattern.compile("-?\d+(\.\d+)?\n");
if (possibleNumberAsString == null) {
return false;
}
return pattern.matcher(possibleNumberAsString.trim()).matches();
}
private int getRandomNumber(int maximum) {
return ThreadLocalRandom.current().nextInt(0, maximum);
}
private void scrollElementToTheMiddle(WebElement element) {
String scrollElementIntoMiddle = "var viewPortHeight = Math.max(document.documentElement.clientHeight, window.innerHeight || 0);"
+ "var elementTop = arguments[0].getBoundingClientRect().top;"
+ "window.scrollBy(0, elementTop-(viewPortHeight/2));";
((JavascriptExecutor) driver).executeScript(scrollElementIntoMiddle, element);
}