Selenium-Webdriver w /柴作为承诺和摩卡未能等待 [英] Selenium-Webdriver w/ Chai as Promised and Mocha failing to wait

查看:340
本文介绍了Selenium-Webdriver w /柴作为承诺和摩卡未能等待的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的coffeescript测试w / Selenium-Webdriver使用Chai-as-Promised和Mocha,它应该测试一个网页,我使用AJAX调用一旦按下登录按钮进行身份验证:

  selenium = require'selenium-webdriver'
chai = require'chai'
chai.use require 'chai-as-promised'
expect = chai.expect

before - >
@timeout 10000
@driver = new selenium.Builder()
.withCapabilities(selenium.Capabilities.chrome())
.build()
@driver。 getWindowHandle()

after - >
@ driver.quit()

描述'测试登录', - >
beforeEach - >
@ driver.get'http:// DOMAIN / login /'

它'提交登录', - >
@ driver.findElement(id:'email')。sendKeys('foo@bar.com')
@ driver.findElement(id:'password')sendKeys('foo')
@ driver.findElement(css:'#login-btn')。submit()
expect(@ driver.findElement(id:'#login-profile'))。to.eventually.be.true
expect(@ driver.getCurrentUrl())。to.eventually.equal'http:// DOMAIN / profile /'

登录页面的工作方式是,一旦你点击登录一个AJAX调用,如果登录成功的页面重定向通过 document.location.href 到/ profile /.



但是,当这个脚本运行浏览器打开并进入登录页面时,它会正确填写,但是一旦点击它失败。



我认为发生了什么是浏览器不等待AJAX​​调用和随后的重定向的结果,但我虽然promises的整个想法是最终会等待直到有一些超时时间,以验证#login-profile 将需要显示。



我需要明确添加吗?如果是,如何?

解决方案

我能够使用等待语法工作:



@ driver.wait( - > driver.isElementPresent selenium.By.id(login-profile),10000)



感谢此信息



全部测试:

  > 
driver = @driver
@ driver.findElement(id:'email')sendKeys('foo@bar.com')
@ driver.findElement(id:'password')。 send();
@ driver.wait( - > driver.isElementPresent selenium.By.id(登录配置文件),10000)
expect(@ driver.getCurrentUrl())。to.eventually.equal'http:// DOMAIN / profile /'
pre>

I've got a simple coffeescript test w/ Selenium-Webdriver using Chai-as-Promised and Mocha which is supposed to test a web page that I have that uses an AJAX call to do authentication once a login button is pressed:

selenium = require 'selenium-webdriver'
chai = require 'chai'
chai.use require 'chai-as-promised'
expect = chai.expect

before ->
  @timeout 10000
  @driver = new selenium.Builder()
    .withCapabilities(selenium.Capabilities.chrome())
    .build()
  @driver.getWindowHandle()

after ->
  @driver.quit()

describe 'Test Login', ->
  beforeEach ->
    @driver.get 'http://DOMAIN/login/'

  it 'submitting login', ->
    @driver.findElement(id: 'email').sendKeys('foo@bar.com')  
    @driver.findElement(id: 'password').sendKeys('foo')
    @driver.findElement(css: '#login-btn').submit()
    expect(@driver.findElement(id: '#login-profile')).to.eventually.be.true
    expect(@driver.getCurrentUrl()).to.eventually.equal 'http://DOMAIN/profile/'

The way the login page works is that once you click login an AJAX call is made, and if login is successful the page is redirected via document.location.href to /profile/.

However, when this script runs the browser opens and goes to the login page, it's filled out correctly, but then as soon as it's clicked it fails.

I think what's happening is the browser is not waiting for the result of the AJAX call and subsequent redirect, but I though the whole idea of promises would be that eventually would wait until some timeout period to verify that #login-profile would need to show up.

Do I need to add that explicitly? If so, how?

解决方案

I was able to get this to work using the wait syntax:

@driver.wait(-> driver.isElementPresent selenium.By.id("login-profile"), 10000)

thanks to this post.

The full test:

it 'submitting login', ->
    driver = @driver
    @driver.findElement(id: 'email').sendKeys('foo@bar.com')  
    @driver.findElement(id: 'password').sendKeys('foo')
    @driver.findElement(css: '#login-btn').submit()
    @driver.wait(-> driver.isElementPresent selenium.By.id("login-profile"), 10000)
    expect(@driver.getCurrentUrl()).to.eventually.equal 'http://DOMAIN/profile/'

这篇关于Selenium-Webdriver w /柴作为承诺和摩卡未能等待的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆