1

I want to pass or fail the tests of "Login" function based on fields available on the second page. I used "xpath" to capture the fields. But it does not capture the fields of the second page. I suppose the reason is the second page is not loaded when the code tries to check the fields of the second page. Because when I tried to capture one of the fields of the Login page, it does not show an error message.

login_page.ts driver: any;

//Define Locators
_email_field = "user_email";
_password_field = "user_password";
_login_button ="commit"
_login_url: string = 'https://sso.teachable.com/secure/42299/users/sign_in?clean_login=true&reset_purchase_session=1';


constructor(){  
    this.driver = new webdriver.Builder().withCapabilities(webdriver.Capabilities.chrome()).build();

    this.driver.get(this._login_url).then(function (err:any) {
        console.log(`Page opened`);            
    })

}

enterEmail(email:any){
    this.driver.findElement(By.id(this._email_field)).sendKeys(email);        
}  

enterPassword(password:any){
    return this.driver.findElement(By.id(this._password_field)).sendKeys(password);
}

clickLoginButton(){
    return this.driver.findElement(By.name(this._login_button)).click();
}

public login(email:any, password:any) {

     return this.driver.getCurrentUrl().then(() =>{           

        this.enterEmail(email);
        this.enterPassword(password);
        this.clickLoginButton()

        console.log(`login button clicked`);  

    })

}public verifyLoginSuccessful()
{

    try{
        this.driver.findElement(By.xpath(".//*[@id='new_user']/div[1]/label")); //This field available on login page
        this.driver.findElement(By.xpath(".//*[@id='nav-bar']//span[text()='User Settings']"));           
        this.driver.findElement(By.xpath(".//*[@id='search-courses']"));
        console.log("Element Found");
    }
    catch(e){
        console.log("Element Not Found");
    }

login.test.ts

 it('Should log into kodeit' ,async()=>{
        await loginPage.login("test@email.com", "abcabc").then(()=> {
            console.log(`Return to test after logged in`);  
        }).then(()=> {
               let result = loginPage.verifyLoginSuccessful();
               expect(result).toBeTruthy();
    })   
1

I could found a solution.

login_page.ts

driver: ThenableWebDriver; 

findWelcomeMessageField(){
    return this.driver.wait(until.elementLocated(By.id('welcome')), 2000);
}

public async verifyLoginSuccessful()
{
    const result = await this.findWelcomeMessageField();
    if (result != null ){
        console.log("Element Found");
        return true;
    }   
    else{
        console.log("Element Not Found");
        return false;
    }          
}

This worked for me. I added the answer in case if anyone faces the same issue.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.