繁体   English   中英

Selenium-Webdriver 显式等待 Apache JMeter 5.5

[英]Selenium-Webdriver Explicit Wait Apache JMeter 5.5

我在 Apache Jmeter 5.5 中使用 Selenium Webdriver 并要求使用显式等待。脚本语言是 Java。这是我的示例代码:

import org.openqa.selenium.*;
import org.openqa.selenium.support.ui.*;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

var driver=WDS.browser;

// Load Url
driver.get("https://samplewebsite.com");
driver.manage().window().setSize(new Dimension(1936, 1048));
driver.manage().window().maximize();
driver.findElement(By.xpath(".//span[@class='content']")).click();

// Login 
WebElement username = new WebDriverWait(driver, Duration.ofSeconds(10)).until(ExpectedConditions.elementToBeClickable(By.xpath(".//input[@type='email']")));
driver.findElement(By.xpath(".//input[@type='email']")).click();
driver.findElement(By.xpath(".//input[@type='email']")).sendKeys("sampleUsername");
driver.findElement(By.xpath(".//input[@type='submit']")).click();
driver.findElement(By.xpath(".//input[@type='password']")).click();
driver.findElement(By.xpath(".//input[@type='password']")).sendKeys("samplePassword");
driver.findElement(By.xpath(".//input[@type='submit']")).click();
driver.findElement(By.cssSelector("span")).click();
// login details
WDS.sampleResult.sampleEnd();

在上面的代码中,我遇到了下面没有这样的元素异常:

目标异常:org.openqa.selenium.NoSuchElementException:没有这样的元素:无法定位元素:{“method”:“xpath”,“selector”:“.//input [@type ='email']”}

块引用

因此,我按照建议使用了显式等待,但是,我面临以下错误:

 Sourced file: inline evaluation of: ``import org.openqa.selenium.*; import org.openqa.selenium.support.ui.*; import or . . . '' : Typed variable declaration : Attempt to resolve method: ofSeconds() on undefined variable or class name: Duration : at Line: 15 : in file: inline evaluation of: ``import org.openqa.selenium.*; import org.openqa.selenium.support.ui.*; import or . . . '' : Duration .ofSeconds ( 10 ) 
 in inline evaluation of: ``import org.openqa.selenium.*; import org.openqa.selenium.support.ui.*; import or . . . '' at line number 15

所有 xpath 都经过验证且正确无误,我无法找到上述错误的解决方案。 对于我正在进行的任何故障排除,我都面临这个源文件评估错误。

我的目标是在我们分别在 email 和密码字段中输入用户名和密码详细信息之前进行正确的显式等待。

// Login 
WebElement username = new WebDriverWait(driver, Duration.ofSeconds(10)).until(ExpectedConditions.elementToBeClickable(By.xpath(".//input[@type='email']")));
driver.findElement(By.xpath(".//input[@type='email']")).click();
driver.findElement(By.xpath(".//input[@type='email']")).sendKeys("sampleUsername");
driver.findElement(By.xpath(".//input[@type='submit']")).click();
driver.findElement(By.xpath(".//input[@type='password']")).click();
driver.findElement(By.xpath(".//input[@type='password']")).sendKeys("samplePassword");
driver.findElement(By.xpath(".//input[@type='submit']")).click();
driver.findElement(By.cssSelector("span")).click();

您需要(至少)导入持续时间class:

import java.time.Duration

也不要与java混淆,因为如果你认为它是“java” - 它不是,你实际上使用的是不是 100% Java 兼容的Beanshell 解释器

如果你想使用更接近 Java 的东西 - 考虑切换到groovy语言,而且它是自 JMeter 3.1 以来推荐的脚本选项

暂无
暂无

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

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