My code snippet looks like this:
public async waitForElementSelected(element: WebdriverIO.Element) {
/**
* Maximum number of milliseconds to wait for
* @type {Int}
*/
const ms = 10000;
await browser.waitUntil(async () =>{
return element.isSelected();
},{
timeout: ms,
timeoutMsg: 'Condition not met: Element is not selected',
interval: 100
});
}
While I was able to easily test the wait until function by mocking the browser, I encountered difficulties in mocking the "isSelected" or element.isSelected() line.
I attempted to mock it with:
global.element = {
isSelected: jest.fn()
};
However, this approach did not work as expected and the line element.isSelected() remained uncovered.
Below is my Test case:
describe('waitForElementToBeSelected', () => {
beforeEach(() => {
mockElement = {
isSelected: jest.fn()
};
mockElement.isSelected.mockReturnValue(true);
/** **browser mock section ***/
// global.browser = {
// waitUntil: ()=> {
// mockElement.isSelected();
// }
// };
});
it('should call waitForElementSelected on the browser object', async () => {
await waitActions.waitForElementToBeSelected(mockElement);
expect(mockElement.isSelected).toHaveBeenCalled();
//
//expect(global.browser.waitUntil).toHaveBeenCalledTimes(1);
});
});
This code resulted in an error stating "browser not defined". Enabling the code block,"/** **browser mock section ***/", leads to the issue where the line
return element.isSelected()
remains uncovered in the testing.