简体   繁体   English

使用 Selenium Java 我想自动化一个包含价格表和价格计算器的表格

[英]Using Selenium Java I want to automate a table that contains a price list and a price calculator

Problem: Using Selenium Java I want to automate a table that contains a price list and a price calculator问题:使用 Selenium Java 我想自动化一个包含价格表和价格计算器的表格

Another little problem: I can't click on the "Analysis" (Filter) button, because I have to double-click, which I could do in this case另一个小问题:我无法点击“分析”(过滤器)按钮,因为我必须双击,在这种情况下我可以这样做在此处输入图片说明

What I want: I'm trying to find a method so that when I run the program, the program will click on another analysis (on another + button) Now with the help of a selector, I can only click on the first (+).我想要什么:我试图找到一种方法,以便当我运行程序时,程序将单击另一个分析(在另一个 + 按钮上)现在在选择器的帮助下,我只能单击第一个(+ )。 But I want each time, the program to test, randomly other analyzes (other buttons +).但我希望每次,程序进行测试,随机进行其他分析(其他按钮+)。

What I do我做的事情

@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();
    }

Link: https://www.poliana.ro/analize-preturi/链接: https : //www.poliana.ro/analize-preturi/

I've added below the code to click on a random analyze and to calculate the total:我在代码下方添加了点击随机分析并计算总数:

@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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM