繁体   English   中英

Jasmine为什么期望量角器总是返回true(Web UI自动化测试)

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM