[英]Why did Jasmine expect always return true in protractor (Web UI Automation Test)
我正在為angular 2網站自動化Web UI測試。 但是,我想檢查登錄步驟是否成功時遇到問題。 即使我輸入了錯誤的密碼或錯誤的XPath注銷按鈕,它也總是可以通過。
當我們成功登錄時,此元素才存在。 正如我之前提到的,我故意為btt_Logout輸入錯誤的密碼,甚至輸入錯誤的xpath(xbutton不是按鈕),因此該元素不能存在,但仍然適用。
即使嘗試了很多事情,我也不明白是什么問題。 我只是將toBe(true)更改為toBe(false),但仍然可以通過:)。 似乎這種期望不起作用。 我不明白為什么
import {browser, ExpectedConditions as EC, $, $$, element,by} from 'protractor'
import { Login } from '../page_objects/login.page';
import { helper } from '../helpers/helper'
declare let expect:any;
describe('Testing Login ', function () {
beforeEach(async () => {
})
it('Go to loginpage', async function () {
//Go to Login Page
Login.openHomePage();
});
it('Login to the page', async function () {
//Enter username, password and press OK. It work well.
Login.login("admin","admin@123");
//Waiting to see btt_Logout. Problems in here. It always true even i make XPath of btt_Logout wrong or make password wrong. I check/
browser.wait(EC.presenceOf(Login.btt_Logout), 30000).then(function () {
expect((Login.btt_Logout).isPresent()).toBe(true);
}, function (error) {
expect(true).toBe(false);
});
});
afterAll(()=> {
helper.cleanUp();
console.warn(`Test finished!`)
})
})
登錄頁面對象:
import {browser, ExpectedConditions as EC, $, $$, element, by, ElementFinder} from 'protractor'
import {BasePage} from './base.page'
require('../helpers/waitReady.js');
class LoginPage extends BasePage {
//Internal element
protected txt_LoginUsername: ElementFinder = element(by.id('username'));
protected txt_LoginPassword: ElementFinder = element(by.id('password'));
protected btt_LoginSubmit: ElementFinder = element(by.xpath("//button[contains(@class,'btn-submit')]"));
//External element
public login(Username, Password) {
this.txt_LoginUsername.sendKeys(Username);
this.txt_LoginPassword.sendKeys(Password);
this.btt_LoginSubmit.click();
}
}
export const Login = new LoginPage();
基本頁面:
import {browser, ExpectedConditions as EC, $, $$, element, by, ElementFinder} from 'protractor'
require('../helpers/waitReady.js');
export abstract class BasePage {
protected url: string = '' // Will be same as baseUrl by default.
public btt_Logout: ElementFinder = element(by.xpath("//xbutton[contains(@class,'btn-logout')]"));
async openHomePage() {
return await browser.get(this.url)
}
async openUrl(webUrl) {
return await browser.get(webUrl)
}
}
結果:開始
茉莉花開場:
»測試登錄▻轉到登錄頁面。 ✓進入登錄頁面(16 s)▻登錄頁面。 ✓登錄頁面(0.234 s)測試完成!
2個規格,0個故障在16.599秒內完成
在17秒內執行2項規格中的2項
PASSED 2 ( 100% )
FAILED 0 ( 0% )
SKIPPED 0 ( 0% )
[22:38:58] I /啟動器-0個WebDriver實例仍在運行[22:38:58] I /啟動器-chrome#01通過
我相信這是因為您在isPresent()
expect((Login.btt_Logout).isPresent()).toBe(true);
它檢查元素是否存在,因此返回true。
您的期望沒有被觸發,更不用說等待使測試變得混亂。 另外, presenceOf
僅檢查元素在dom中,而不檢查它的可見性。
嘗試在您的login()
方法中放置等待login()
無論如何,這可能是正確的地方),然后將注銷按鈕設為公開。 量角器/茉莉將處理在等待你expect
。
import {browser, ExpectedConditions as EC, $, $$, element, by, ElementFinder} from 'protractor'
import {BasePage} from './base.page'
require('../helpers/waitReady.js');
class LoginPage extends BasePage {
//Internal element
protected txt_LoginUsername: ElementFinder = element(by.id('username'));
protected txt_LoginPassword: ElementFinder = element(by.id('password'));
protected btt_LoginSubmit: ElementFinder = element(by.xpath("//button[contains(@class,'btn-submit')]"));
public btt_Logout: ElementFinder = element(by.xpath("//xbutton[contains(@class,'btn-logout')]"));
//External element
public login(Username, Password) {
this.txt_LoginUsername.sendKeys(Username);
this.txt_LoginPassword.sendKeys(Password);
this.btt_LoginSubmit.click();
return browser.wait(EC.visibilityOf(this.btt_Logout), 30000, 'timeout: waiting for login');
}
}
export const Login = new LoginPage();
那么您的測試將是...
it('Login to the page', async function () {
Login.login("admin","admin@123");
expect(Login.btt_Logout.isDisplayed()).toBe(true);
});
我剛剛發現我的問題是我將元素login_button放在base_page中,這引起了很大的問題。 當量角器打開時,它將在尋找該元素,這導致我們必須等待很長時間。 – Mike,現在編輯
您是否檢查過HTML / CSS中是否存在某些對象?
在某些情況下,如果您要檢查TEXT,
該元素可能仍然存在,並且僅顯示EMPTY值,
這就是為什么它看起來不可見而是isPresent和isDisplayed的原因。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.