我正在使用Selenium 2 WebDriver来测试使用AJAX的UI.
有没有办法让驱动程序等待Ajax请求完成.
基本上我有这个:
d.FindElement(By.XPath("//div[8]/div[3]/div/button")).Click();
// This click trigger an ajax request which will fill the below ID with content.
// So I need to make it wait for a bit.
Assert.IsNotEmpty(d.FindElement(By.Id("Hobbies")).Text);
Run Code Online (Sandbox Code Playgroud) 我正在使用Selenium WebDriver(Java)和TestNG在我创建的网站上进行一些测试.在这个网站上,我也有JavaScript,在一些函数中,它返回值并通过输出值输出到浏览器控制台console.log().
我想知道Selenium WebDriver是否有一种简单的方法可以访问这些JavaScript信息,因此我可以使用TestNG执行断言.
我对Selenium很新,但我知道你可以这样做:
WebDriver driver = new ChromeDriver();
driver.findElement(By.id("btn")).click();
Run Code Online (Sandbox Code Playgroud)
那么我可以用什么类似的东西WebDriver来阅读网站上的JavaScript?
看起来人们假设我正试图通过Selenium"执行"JavaScript代码.
不是这样的.相反,我正在尝试使用Selenium存储已定义的JavaScript变量.
基本上,我希望Selenium能够获取JavaScript变量的值,将其存储在本地,然后对其进行断言测试.
说我的网站有以下JS代码:
$(document).ready(function() {
var foo = $(#"input-field-val").val();
function returnFoo() {
return foo;
}
});
Run Code Online (Sandbox Code Playgroud)
根据我的阅读和理解,在我单独的Selenium测试文件(Selenium.java)中,我应该可以做这样的事情吗?:
public class Selenium {
WebDriver driver = new FirefoxDriver();
JavascriptExecutor js = (JavascriptExecutor) driver;
@Test
public void testSample() {
driver.get("www.mywebsite.com");
js.executeScript("alert(returnFoo());");
}
}
Run Code Online (Sandbox Code Playgroud)
我做了类似于上面的事情,但没有弹出警报框.相反,我收到一条错误消息:
Exception in thread "main" org.openqa.selenium.WebDriverException: ReferenceError: returnFoo is not defined
Run Code Online (Sandbox Code Playgroud)
当我说JS变量时,我很确定我不明白它意味着什么
不应该是闭包或局部变量的一部分
我也尝试在上面定义一个全局变量,$(document).ready(function()...并且设置在function returnFoo()但仍然不起作用.
我已经移动了两个 …
假设我有input一个表单(看起来像一个按钮并像按钮一样交互),它生成一些数据(好吧,服务器根据表单参数生成数据,但对于用户,按钮就是:) :)基于关于表格中的参数.
当我使用时click(),整个过程挂起(它实际上冻结,没有异常或错误).
来自Selenium网站:
// Now submit the form. WebDriver will find the form for us from the element
element.submit();
Run Code Online (Sandbox Code Playgroud)
所以WebDriver有一个submit()方法.在使用click()按钮或submit()?之间,逻辑上是否有任何区别?
我仍然在学习并回答我的一个问题:在这里,我被告知它可能是因为有问题的元素不在视野中.
我查看了文档,所以这里是最相关的答案:这里
您可以使用"org.openqa.selenium.interactions.Actions"类移动到元素:
WebElement element = driver.findElement(By.id("my-id"));
Actions actions = new Actions(driver);
actions.moveToElement(element);
## actions.click();
actions.perform();
Run Code Online (Sandbox Code Playgroud)
当我尝试使用上面的内容滚动到元素时:它说WebElement没有定义.
我想这是因为我没有导入相关模块.有人可以指出我应该导入的内容吗?
编辑:正如alecxe所指出的,这是java代码.
但与此同时,在试图弄清楚它一段时间之后.我找到了WebElement的导入方法:
from selenium.webdriver.remote.webelement import WebElement
Run Code Online (Sandbox Code Playgroud)
可能会帮助像我这样的人.
如何,这也是一个很好的教训,IMO:
去:文档 的
class selenium.webdriver.remote.webelement.WebElement(parent, id_, w3c=False)
Run Code Online (Sandbox Code Playgroud)
需要分成上面提到的命令格式.
今天运行我的rspec测试,每当测试中的某个地方出现`page.execute_script'调用时,我都会收到以下错误.
Selenium::WebDriver::Error::JavascriptError:
waiting for evaluate.js load failed
# [remote server] file:///tmp/webdriver-profile20130807-3105-fpynb7/extensions/fxdriver@googlecode.com/components/driver_component.js:8360:in `r'
# [remote server] file:///tmp/webdriver-profile20130807-3105-fpynb7/extensions/fxdriver@googlecode.com/components/driver_component.js:392:in `fxdriver.Timer.prototype.runWhenTrue/g'
# [remote server] file:///tmp/webdriver-profile20130807-3105-fpynb7/extensions/fxdriver@googlecode.com/components/driver_component.js:386:in `fxdriver.Timer.prototype.setTimeout/<.notify'
Run Code Online (Sandbox Code Playgroud)
在上面路径的/ resources目录(而不是组件)中有一个文件evaluate.js,就像在其他机器上一样.
这是在从22更新到Firefox 23之后发生的.我还没有能够回滚确认返回22确实解决了问题,但是我相信这一切都已经改变了.
有没有人见过这个问题?
运行Kubuntu 12.04,Capybara 1.1.4,selenium-webdriver gem 2.33.0
我尝试将Capybara更新为2.whatever和selenium-webdriver到2.34.0,没有变化.
我正在尝试让Selenium等待页面加载后动态添加到DOM的元素.试过这个:
fluentWait.until(ExpectedConditions.presenceOfElement(By.id("elementId"));
Run Code Online (Sandbox Code Playgroud)
如果它有帮助,这里是fluentWait:
FluentWait fluentWait = new FluentWait<>(webDriver) {
.withTimeout(30, TimeUnit.SECONDS)
.pollingEvery(200, TimeUnit.MILLISECONDS);
}
Run Code Online (Sandbox Code Playgroud)
但它会抛出NoSuchElementException- 看起来像presenceOfElement期望元素存在,所以这是有缺陷的.这对于Selenium来说一定是面包和黄油,并且不想重新发明轮子......任何人都可以建议一个替代品,理想情况下不要自己动手Predicate吗?
我现在正在学习Selenium,遇到了一个问题.
我知道Selenium默认支持旧的Firefox版本,没有驱动程序.对于最新版本的Firefox,我们必须下载驱动程序并使用它来定义它System.setProperty.
根据此链接,对于Firefox 45和46,启动驱动程序代码可能如下所示:
WebDriver driver = new FirefoxDriver();
Run Code Online (Sandbox Code Playgroud)
我的Firefox版本为45.5.1.但是上面的代码仍然不起作用.所以根据这个链接,我添加了:
System.setProperty("webdriver.firefox.marionette","C:\\geckodriver.exe");
它奏效了.
然后我意识到我没有安装geckodriver.exe在我的电脑上.要了解它是如何进行的,我已经更改为以下代码:
System.setProperty("webdriver.firefox.marionette","");
Run Code Online (Sandbox Code Playgroud)
它仍然有效.
所以,这是我的第一个问题:发生了什么?我确信geckodriver.exe在我的环境中不存在.如果没有指出位置,那我为什么要设置属性?
另外,我见过的代码如下:
System.setProperty("webdriver.gecko.driver", "/tools/marionette/wires.exe");
Run Code Online (Sandbox Code Playgroud)
我的第二个问题是,是什么样的区别webdriver.gecko.driver 和webdriver.firefox.marionette或wires.exe和geckodriver.exe?
selenium selenium-firefoxdriver selenium-webdriver firefox-marionette geckodriver
来源:JUnit 5、Eclipse 4.8、Selenium
我可以在没有任何特殊测试框架的情况下编写和执行 Selenium 脚本,但我想使用 Junit 5(因为我们依赖其他工具)并且我从未见过这样的错误“org.junit.jupiter.api.extension.ParameterResolutionException”而与 Junit 4 一起工作。目前它是 Junit 5,我用谷歌搜索它以获得某种想法,但无法解决问题。
测试脚本:
package login;
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
public class loginTest {
public WebDriver driver = null ;
public loginTest(WebDriver driver)
{
this.driver=driver;
}
@BeforeEach
public void setUp() throws Exception
{
driver.get("google.com");
System.out.println("Page title is: " + driver.getTitle());
}
@Test
public void test() {
// some action here I have in original script
System.out.println("Page title is: " …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用以下代码处理身份验证弹出窗口:
FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("network.http.phishy-userpass-length", 255);
profile.setPreference("network.automatic-ntlm-auth.trusted-uris", "x.x.x.x");
driver = new FirefoxDriver(profile);
baseUrl="http://" + login + ":" + password + "@" + url;
driver.get(baseUrl + "/");
Run Code Online (Sandbox Code Playgroud)
当我执行测试时,页面显示身份验证弹出窗口,并且仍然加载直到我单击取消按钮.那一刻,我可以访问下一页,这意味着身份验证成功但仍然始终显示身份验证弹出窗口
截至本文发布之日,"Microsoft Edge"这个名称刚刚被正式宣布为新Windows 10的默认浏览器.
现在提问可能为时过早,但我想知道是否可以使用新的Selenium WebDriver,如果没有,是否有任何说明我们可以期待等待多长时间才能看到一个已开发的?
(Windows 10的技术预览已经出来,所以这对我来说似乎不是一个愚蠢的问题.)
selenium webdriver selenium-webdriver microsoft-edge selenium-edgedriver