我需要运行在我正在开发一款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()
})
})
这种失败并带我到Next
按钮
from selenium import webdriver
import time
# variables for userid & password
u = "[email protected]"
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)
任何指针?
在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
请求。即,它不被任何安全约束。
一旦你登录周围以适当的端到端测试,没有理由继续cy.visit()
登录并等待整个页面运行任何其他命令之前加载所有相关的资源。这样做可以放慢我们的整个测试套件。使用cy.request()
,我们可以绕过这一切,因为它会自动获取和设置饼干就好像请求从浏览器本身就来了。
因此一个POST请求被与JSON有效载荷或体一起发送。现在,在成功地验证请求主体,我们的应用程序代码中提取出令牌有效载荷,并将其保存在本地存储和还增加了令牌中的所有请求头,这样的API服务器可以验证从我们的应用程序来后续请求。
当我们的应用程序加载时,它会检查该令牌已经在本地存储或不是,如果是则它会开始,并设置它的请求代理。通过这种方式,它会绕过登录部分,当你将测试GSuite。