我需要在我正在开发的Chrome扩展程序上运行自动化测试。 第一步是验证/登录GSuite日历。
我正在使用赛普拉斯,但不允许我登录到GSuite日历。取而代之的是(从Cypress中)单击“单击登录”时,它再次跳至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()
})
})
此操作失败,将我带到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)
有指针吗?
答案 0 :(得分:3)
在spec.js
文件中,添加以下代码:
describe('Random VMR for ESG',function() {
beforeEach(() => {
cy.visit('https://www.google.com/calendar')
})
it('type email & password', function() {
cy.get('#Email').type('my user')
cy.get('#next').click()
cy.get('#Passwd').type('my password')
cy.get('#signIn').click()
})
})
在support
文件夹的command.js
中,添加以下代码:
Cypress.Commands.add('login', ()=> {
cy.request({ // cy.request is not bound by any security and will bypass the login part
method: 'POST' , // Post request to URL along with body
url: 'https://www.google.com/calendar',
body: {
user: {
email: 'my user',
password: 'my password',
}
}
})
//server sends back the response JSON payload
.then((resp) => { //token extracted from JSON payload and is saved in LocalStorage
window.localStorage.setItem('jwt' , resp.body.user.token)
})
})
在spec.js
文件中,有一些代码可以访问URL并测试email
和password
输入类型并登录。
问题是登录后立即将页面重定向回首页。要解决此问题,由于cy.request
请求,我们在commands.js
文件中使用cy.request
不受浏览器限制。 即它不受任何安全性的约束。
一旦您对登录进行了正确的 e2e 测试,则没有理由继续cy.visit()
登录并等待整个页面加载所有相关资源,然后再运行其他命令。这样做会减慢我们整个测试套件的速度。使用cy.request()
,我们可以绕开所有这些,因为它会自动获取并设置cookie ,就像请求来自浏览器本身一样。
因此,已发送 POST 请求以及JSON有效负载或正文。现在,成功验证请求主体后,我们的应用程序代码从有效负载中提取令牌并将其保存在本地存储中,还将令牌添加到所有请求标头中,以便API服务器可以验证后续的请求从我们的应用程序。
当我们的应用程序加载时,它会检查令牌是否已在本地存储中(如果已存在),那么它将继续并将其设置在请求代理上。这样,当您测试 GSuite 时,它将绕过登录部分。