0
votes

I have an exercise that cant solve

Check that the page loaded is the expected one. 

a. Yes: Continue with the test. 

b. No: Return an Error notifying that the page loaded is not the expected one.

How can write these conditions below in my test case?

I am 100% sure its wrong but cant find the correct way how to write it.

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")
        }
    });  
});
1
what's the problem with your script? any errors? Anything goes not as you expect? What are we solving?Sergey Pleshakov

1 Answers

0
votes

The simple if-else statement to check that URL is the expected one:

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')
    }
});

But I would recommend writing it with the 'expect' function:

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

    await browser.get(url);

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

Also, you may need to wait for the URL changes. There are some methods for it:

// 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);