use*_*164 5 selenium node.js cypress
我需要在正在开发的Chrome扩展程序上运行自动化测试。第一步是验证/登录GSuite日历。
我正在使用赛普拉斯,但不允许我登录到GSuite日历。而是(从赛普拉斯)“单击登录”时,它将Next再次跳至该按钮。
describe('Login',function() {
it('Go to GSuite calendar', function() {
cy.visit('https://www.google.com/calendar')
})
it('Login', function() {
cy.get('#Email').type('my user')
cy.get('#next').click()
cy.get('#Passwd').type('my password')
cy.get('#signIn').click()
})
})
Run Code Online (Sandbox Code Playgroud)
这失败,将我带到Next按钮
from selenium import webdriver
import time
# variables for userid & password
u = "JohnDoe@xxxx-labs.com"
p = "Mysecretpassword"
# chromedriver installation required for login through google chrome
driverpath = "C:/Users/pjain2/Desktop/chromedriver_win32/chromedriver"
# launch google calendar login page
d = webdriver.Chrome(driverpath)
d.get("https://www.google.com/calendar")
# fill email field send_keys for filling text
e = d.find_element_by_id("identifierId")
e.send_keys(u)
# find & click next button
d.find_element_by_xpath("//*[@id='identifierNext']/content/span").click()
time.sleep(2)
# enter password
e = d.find_element_by_xpath("//*[@id='password']/div[1]/div/div[1]/input")
e.send_keys("Mysecretpassword")
time.sleep(2)
# sign in
d.find_element_by_xpath("//*[@id='passwordNext']/content/span").click()
time.sleep(10)
Run Code Online (Sandbox Code Playgroud)
有指针吗?
在spec.js文件中,添加以下代码:
describe('Random VMR for ESG',function() {\n beforeEach(() => {\n cy.visit('https://www.google.com/calendar')\n })\n it('type email & password', function() {\n cy.get('#Email').type('my user')\n cy.get('#next').click()\n cy.get('#Passwd').type('my password')\n cy.get('#signIn').click()\n }) \n})\nRun Code Online (Sandbox Code Playgroud)\n\n在support文件夹中command.js,添加以下代码:
Cypress.Commands.add('login', ()=> {\n cy.request({ // cy.request is not bound by any security and will bypass the login part\n method: 'POST' , // Post request to URL along with body\n url: 'https://www.google.com/calendar',\n body: {\n user: {\n email: 'my user',\n password: 'my password',\n }\n }\n })\n //server sends back the response JSON payload \n .then((resp) => { //token extracted from JSON payload and is saved in LocalStorage\n window.localStorage.setItem('jwt' , resp.body.user.token)\n })\n})\nRun Code Online (Sandbox Code Playgroud)\n\n文件中包含仅访问 URL 并测试输入类型和登录的spec.js代码。emailpassword
问题是登录后立即将页面重定向回首页。为了解决此问题,我们cy.request在commands.js文件中使用了cy.request超出浏览器限制的请求。 即它不受任何安全约束。
一旦您对登录进行了适当的端到端测试,就没有理由继续cy.visit()登录并等待整个页面加载所有关联的资源,然后再运行任何其他命令。这样做会减慢我们整个测试套件的速度。使用cy.request(),我们可以绕过所有这些,因为它会自动获取和设置 cookie,就像请求来自浏览器本身一样。
因此,POST请求与 JSON 有效负载或正文一起发送。现在,在成功验证请求正文后,我们的应用程序代码从有效负载中提取令牌并将其保存在本地存储中,并将令牌添加到所有请求标头中,以便 API 服务器可以验证来自我们应用程序的后续请求。
\n\n当我们的应用程序加载时,它会检查令牌是否已在本地存储中,如果是,则继续并将其设置在请求代理上。这样,当您测试GSuite时,它将绕过登录部分。
\n| 归档时间: |
|
| 查看次数: |
498 次 |
| 最近记录: |