Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/367.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何使用数据表验证同一页面中的所有元素_Java_Selenium Webdriver_Cucumber - Fatal编程技术网

Java 如何使用数据表验证同一页面中的所有元素

Java 如何使用数据表验证同一页面中的所有元素,java,selenium-webdriver,cucumber,Java,Selenium Webdriver,Cucumber,我想知道如何找到同一页面中的所有元素,并使用Selenium/Java/Cucumber中的数据表断言它们在页面中的显示 例如,我有一个这样的场景 Sceanario: Verify all the elements in the xyz page Given I am in the abc page When I navigate to xyz page Then I can see the following fields in the xyz page |field 1| |field 2

我想知道如何找到同一页面中的所有元素,并使用Selenium/Java/Cucumber中的数据表断言它们在页面中的显示

例如,我有一个这样的场景

Sceanario: Verify all the elements in the xyz page
Given I am in the abc page
When I navigate to xyz page
Then I can see the following fields in the xyz page
|field 1|
|field 2|
|field 3|
|field 4|

如果将所有值放置到数组中会怎么样
{字段1、字段2、字段3、字段4}
下一步->将检查页面上的每个值的可见性


我认为应该解决这个问题。< /P> < P> <强>第一步:< /强>构建数据表。(线索,使用标题我们可以以非常干净和精确的方式实现数据表,并且考虑到数据表如下所示)

第二步:实施步骤定义

    @Then    
    public void I_can_see_the_following_fields_in_the_xyz_page(DataTable table) throws Throwable {
        WebElement element;
        List<Map<String, String>> list = table.asMaps(String.class,String.class); 
        for(Map<String, String> list : data) {
            element = driver.findElement(By.xpath(list.get("Locator")));
            Assert.assertTrue("Element : "+list.get("Field Name")+ "not found",isElementPresent(element));   
        }
    }
那么 public void我可以在页面(DataTable table)中看到以下字段{ 网络元素; List=table.asMaps(String.class,String.class); 用于(地图列表:数据){ element=driver.findElement(By.xpath(list.get(“Locator”)); Assert.assertTrue(“元素:”+list.get(“字段名”)+“未找到”,isElementPresent(元素)); } } 实用方法:检查元素是否存在

    protected synchronized boolean isElementPresent(WebElement element) {
        boolean elementPresenceCheck = false;
        Wait<WebDriver> wait=null;
        try {
            wait = new FluentWait<WebDriver>((WebDriver) driver).withTimeout(10, TimeUnit.SECONDS).pollingEvery(1,
                    TimeUnit.SECONDS);
            elementPresenceCheck = wait.until(ExpectedConditions.visibilityOf(element)).isDisplayed();
            return elementPresenceCheck;
        }catch(Exception e) {
            return elementPresenceCheck;
        }
    }
受保护的同步布尔值isElementPresent(WebElement元素){
布尔元素presencecheck=false;
等待=null;
试一试{
wait=新的FluentWait((WebDriver)驱动程序)。带有超时(10,时间单位。秒)。轮询间隔(1,
时间单位(秒);
elementPresenceCheck=wait.until(ExpectedConditions.visibilityOf(element)).isDisplayed();
返回元素presencecheck;
}捕获(例外e){
返回元素presencecheck;
}
}

请添加您的发现。感谢Mebin对场景进行格式化,我也尝试过这样做,但我做不到。你能告诉我你是怎么格式化的吗?很简单。参考,谢谢@user11630745您能接受这个答案吗?因为它对其他人也有帮助。
    protected synchronized boolean isElementPresent(WebElement element) {
        boolean elementPresenceCheck = false;
        Wait<WebDriver> wait=null;
        try {
            wait = new FluentWait<WebDriver>((WebDriver) driver).withTimeout(10, TimeUnit.SECONDS).pollingEvery(1,
                    TimeUnit.SECONDS);
            elementPresenceCheck = wait.until(ExpectedConditions.visibilityOf(element)).isDisplayed();
            return elementPresenceCheck;
        }catch(Exception e) {
            return elementPresenceCheck;
        }
    }