backbone.js JavaScript条件量角器测试

i7uq4tfw  于 2022-11-10  发布在  Java
关注(0)|答案(1)|浏览(128)

我有一个无法解决的练习

检查载入的页面是否为预期的页面。

a.是:继续测试。
B.否:返回错误,通知加载的页面不是预期的页面。
怎样才能把这些条件写在我的测试用例下面呢?
我100%肯定它是错的,但找不到正确的方式来写它。

const {element, by} = require ("protractor")
const {ExpectedConditions} = require('protractor')

describe('wefox task', function(){
    it('login', async function(){
        await browser.get('URL');
        if (window.location.href=='URL')
        {
            const user_input = element(by.css("body > wf-root > wf-public-layout > section > div > div > wf-login-page > section > form > div > div > div.wf-c-field.wf-u-pb-24 > div"))
            const username = element(by.id('user_name'))
            const pass_input = element(by.css("body > wf-root > wf-public-layout > section > div > div > wf-login-page > section > form > div > div > div.wf-c-field.--password.wf-u-pb-32 > div"))
            const password = element(by.id('password'))
            const btn_login = element(by.css('body > wf-root > wf-public-layout > section > div > div > wf-login-page > section > form > div > div > button'))
            const button = element(by.buttonText('Anmeldung'))

            await browser.wait(ExpectedConditions.visibilityOf(username),10*1000)
            await browser.wait(ExpectedConditions.elementToBeClickable(user_input),10*1000)
            await browser.wait(ExpectedConditions.visibilityOf(password),10*1000)
            await browser.wait(ExpectedConditions.elementToBeClickable(pass_input),10*1000)

            await user_input.click ()
            await username.sendKeys ("login")
            await pass_input.click ()
            await password.sendKeys ("password")
            await btn_login.click()

            await browser.sleep(50000);
        } else {
            console.log ("error")
        }
    });  
});
9rygscc1

9rygscc11#

简单的if-else语句用于检查URL是否为预期的URL:

it('should verify the URL', async () => {
    const url = 'http://www.protractortest.org/#/';

    await browser.get(url);

    const expectedUrl = await browser.getCurrentUrl();

    if (expectedUrl === url) {
        //...
        console.log('success')
    } else {
        //...
        console.log('error')
    }
});

但我建议使用'expect'函数编写它:

it('should verify the URL', async () => {
    const url = 'http://www.protractortest.org/#/';

    await browser.get(url);

    expect(await browser.getCurrentUrl()).toBe(url);
});

另外,您可能需要等待URL更改。以下是一些方法:

// Waits for the URL to be 'http://www.protractortest.org/#/'.
await browser.wait(ExpectedConditions.urlIs('http://www.protractortest.org/#/'), 5000);

// Waits for the URL to contain 'protractortest'.
await browser.wait(ExpectedConditions.urlContains('protractortest'), 5000);

相关问题