Alternate solution for timeout

export async function enterUsername(emailID: string){
const emailField = await browser.findElement(‘xpath’, ‘//*[@id=“username”]’);
await browser.elementSendKeys(emailField[‘ELEMENT’], emailID);
}

export async function enterPassword(password: string){
const passField = await browser.findElement(‘xpath’, ‘//*[@id=“password”]’);
await browser.elementSendKeys(passField[‘ELEMENT’], password);
}

export async function login( emailID: string, password: string ): Promise{
await new Promise((res) => setTimeout(() => res('p1'), 10000));
await enterUsername(emailID);
await enterPassword(password);
const loginBtn = await browser.findElement(‘xpath’, ‘//*[@id=“kc-login”]’);
await driver.elementClick(loginBtn[‘ELEMENT’]);
await new Promise((res) => setTimeout(() => res('p1'), 5000));

}

Can anyone tell me the alternative that I can use in my login() function. I don’t want to use timeout but I have to use because when I click on sign in button it takes me to browser link, which takes time sometimes to load due to which it fails to find the emailID and password IDs.
I am using webdriverIO framework with Appium to automate the testCase for my android device.

Could not able to find any alternative in my case. It will be very helpful if someone can shed some light on this. Thanks :slight_smile:

Use implicit wait:

https://appium.io/docs/en/commands/session/timeouts/implicit-wait/

In my case, it is throwing error that setImplicitWaitTimeout() is not a function. So, on further debugging I found out alternative option waitUntil() but this is also not working properly in my case. Below are my modifications:

    export async function enterUsername(emailID: string){
        await browser.waitUntil(
            async () => (await browser.findElement('xpath', '//*[@id="username"]')) != null,
            {
                timeout: 10000,
                timeoutMsg: 'expected element to be different after 5s'
            }
        );
        const emailField = await browser.findElement('xpath', '//*[@id="username"]');
        await browser.elementSendKeys(emailField['ELEMENT'], emailID);
    }

export async function enterPassword(password: string){
    await browser.waitUntil(
        async () => (await browser.findElement('xpath', '//*[@id="password"]')) != null,
        {
            timeout: 10000,
            timeoutMsg: 'expected element to be different after 5s'
        }
    );
    const passField = await browser.findElement('xpath', '//*[@id="password"]');
    await browser.elementSendKeys(passField['ELEMENT'], password);
}

export async function login( emailID: string, password: string ): Promise<void>{
    // await new Promise((res) => setTimeout(() => res('p1'), 10000));
    await enterUsername(emailID);
    await enterPassword(password);
    const loginBtn = await browser.findElement('xpath', '//*[@id="kc-login"]');
    await driver.elementClick(loginBtn['ELEMENT']);
    // await new Promise((res) => setTimeout(() => res('p1'), 5000));
    // await driver.setImplicitWaitTimeout(5000);
}

Error:
Error: Malformed type for “elementId” parameter of command elementSendKeys
[emulator-5554 Android 11 #0-0] Error: Malformed type for “elementId” parameter of command elementSendKeys
[emulator-5554 Android 11 #0-0] Expected: string
[emulator-5554 Android 11 #0-0] Actual: undefined

I suspect due to network connection, migration to the url is taking time due to which sometimes blank screen appears in between that transition and by the time the url loads completely it throws the error.

Maybe your driver is not set up properly? I can’t really tell from your description. Here is the same command at github:

You should add a check to make sure the url is loaded before you check for elements.

In my case await browser.setImplicitTimeout(10000); worked for me.
Thankyou for your input :slight_smile:

Also, do you have any advise for
You should add a check to make sure the url is loaded before you check for elements?

I don’t know much about your particular test framework, but there are a lot of really good ideas here:

https://stackoverflow.com/questions/28799420/how-to-wait-to-activity-using-appium-on-begin-and-during-test-itself