Protractor 量角器:使用API调用登录应用程序
我正试图从Cypress.io移回量角器(Cypress没有我们需要的跨浏览器功能)。Cypress的优点之一是能够进行API调用以登录到应用程序 我想知道如何用量角器来做这个 这是我的登录请求:Protractor 量角器:使用API调用登录应用程序,protractor,Protractor,我正试图从Cypress.io移回量角器(Cypress没有我们需要的跨浏览器功能)。Cypress的优点之一是能够进行API调用以登录到应用程序 我想知道如何用量角器来做这个 这是我的登录请求: async apiLogin(email: string, password: string) { const PostURL = browser.baseUrl + 'api/auth/login' console.log(`Logging in as ${email}`)
async apiLogin(email: string, password: string) {
const PostURL = browser.baseUrl + 'api/auth/login'
console.log(`Logging in as ${email}`)
const body = {
email: email,
password: password,
}
// tslint:disable-next-line: only-arrow-functions
this.request
.post(PostURL)
.set('Content-Type', 'application/json')
.send(body)
// tslint:disable-next-line: only-arrow-functions
.end(function(err: string, response: any) {
if (err) {
console.log(`Login post error = ${err}`)
} else {
console.log(`Login post response = ${response.status}`)
}
})
}
我在量角器上遇到的问题是,我得到了一个成功的请求,但它只是位于登录页面上
这是我的beforeAll()
beforeAll(async () => {
await loginPage.apiLogin(user.email, user.password)
await browser.get(browser.baseUrl + 'explorations/new')
})
我真的不知道我做错了什么 为了进行API调用,我切换到Axios
async loginAPI(email: string, password: string) {
const PostURL = browser.baseUrl + 'api/auth/login'
const requestBody = {
email: email,
password: password,
}
const response = await axios.post(PostURL, requestBody, {
headers: {
'Content-Type': 'application/json',
},
})
const csrfToken = await response.headers['set-cookie'][0].split(';')[0].split('=')[1]
const sessionID = await response.headers['set-cookie'][1].split(';')[0].split('=')[1]
const results = {
csrfToken,
sessionID,
}
return results
}
然后使用它:
beforeAll(async () => {
const sessionInfo = await loginPage.loginAPI(user.email, user.password)
const csrftoken = await sessionInfo.csrfToken
const sessionID = await sessionInfo.sessionID
await browser.manage().addCookie({
name: 'csrftoken',
value: csrftoken,
})
await browser.manage().addCookie({
name: 'sessionid',
value: sessionID,
})
browser.get(browser.baseUrl + 'home')
})
为了进行API调用,我切换到Axios
async loginAPI(email: string, password: string) {
const PostURL = browser.baseUrl + 'api/auth/login'
const requestBody = {
email: email,
password: password,
}
const response = await axios.post(PostURL, requestBody, {
headers: {
'Content-Type': 'application/json',
},
})
const csrfToken = await response.headers['set-cookie'][0].split(';')[0].split('=')[1]
const sessionID = await response.headers['set-cookie'][1].split(';')[0].split('=')[1]
const results = {
csrfToken,
sessionID,
}
return results
}
然后使用它:
beforeAll(async () => {
const sessionInfo = await loginPage.loginAPI(user.email, user.password)
const csrftoken = await sessionInfo.csrfToken
const sessionID = await sessionInfo.sessionID
await browser.manage().addCookie({
name: 'csrftoken',
value: csrftoken,
})
await browser.manage().addCookie({
name: 'sessionid',
value: sessionID,
})
browser.get(browser.baseUrl + 'home')
})