Automation testers using Appium scripts may not be aware that there are multiple web contexts available. Consequently, they often encounter error "Element not found" during automation tests. This problem can be easily resolved by exploring other web contexts that may contain the desired element.
To assist Kobiton automation testers, we introduce the new capability called 'kobiton:web-find': 'all' which can be utilized as the desiredCap parameter in Appium test script.
Add web-find
Learn how to add kobiton:web-find to to your capabilities so your automation script will search all available web contexts for the element.
Locate your config file
First, open your test suite and locate your config file. The exact name and location may vary, but you’ll find it in a similar location:
root
└── src
└── test
└── Config # Java TestNG
└── Config.java # Java JUnit
└── config.js # NodeJS Mocha
Add the capability
In the config file, locate DesiredCapabilities, and add kobiton:web-find: 'all'.
For example, in the Node.js Mocha framework:
const desired_caps = {
sessionName: 'Automation test session',
sessionDescription: 'This is an example',
deviceOrientation: 'portrait',
captureScreenshots: true,
browserName: 'safari',
autoWebview: true,
udid: 'xxxx',
platformName: 'iOS',
kobiton:web-find: 'all'
}
Example script
Here’s an example Node.js script with web-find enabled:
// Start a testing scenario labeled 'Context test'
it('Context test', async () => {
// Set a delay of 5000 milliseconds for each asynchronous operation
await driver.setImplicitWaitTimeout(5000);
// Open the webpage at the given URL
await driver.get('https://the-internet.herokuapp.com/nested_frames')
// Pause the script execution for 3000 milliseconds
await driver.sleep(3000)
try {
// Try to find the element with xpath identifier containing the text 'MIDDLE'
let elem = await driver.element("xpath", "//div[contains(., 'MIDDLE')]")
// If the element is found, log the following to the console
console.log("Found element");
} catch (err) {
// If the element cannot be found, log the following message and error details to the console
console.log("Cannot get element");
console.log(err);
}
})
About web-find
When 'kobiton:web-find': 'all' is declared as a desired capability, instead of searching for the target element in a single web context, it enables the search to span across all web contexts, providing matches for the desired element.
When an element is found, the session automatically switched to the context that contains the element. When users perform a search for a web element, they receive an element ID, and can then proceed straight to working with it without having to manually switch context.
The system continually cycles to the next web context if the current web context is not the one containing the element until the element is found or the timeout period elapses.
If no element is found, the system reverts back to the context it was originally in when entering the call to FindWebElements.
By incorporating this enhanced web element search capability, we aim to streamline your automation testing experience and eliminate the challenges associated with identifying elements in different web contexts.
You still have to switch to a web context (any context) to distinguish it from a native search. After that, you do not have to specify any additional web context as long as the element is found within one of the contexts.
What to do if there are multiple matches returned in the search result?
You can query that match’s current context after a find call.
If you are already familiar with working in multiple contexts and you know which context contains your target element, omit the capability 'kobiton:web-find': 'all' from your desiredCap list, and it will default to the traditional mode.