-
Notifications
You must be signed in to change notification settings - Fork 116
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Not informative 'Unable to locate the element' message #46
Comments
👍 |
If this would be interested for anybody... public static void assertElementIsDisplayed(WebElement element, boolean expectedIsDisplayed) {
//get '@FindBy' locator from the element
By locatorBy = null;
try {
Field locatorField = null;
InvocationHandler innerProxy = Proxy.getInvocationHandler(((Proxy) element));
locatorField = innerProxy.getClass().getDeclaredField("locator");
locatorField.setAccessible(true);
AjaxElementLocator locator = (AjaxElementLocator) locatorField.get(innerProxy);
Field byField = locator.getClass().getSuperclass().getDeclaredField("by");
byField.setAccessible(true);
locatorBy = (By) byField.get(locator);
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (ClassCastException e) { //TODO: refactor: why? somehow HtmlElement objects can't be cast to Proxy...
e.printStackTrace();
}
String errorMsg = element + " element should be displayed? - " + expectedIsDisplayed +
"\nElement locator: " + ((locatorBy != null) ? locatorBy : "Could not get, see sources...");
try {
assertEquals(element.isDisplayed(), expectedIsDisplayed, errorMsg);
} catch (NoSuchElementException e) {
fail(errorMsg + "\n" + e + "\n" + e.getStackTrace());
} |
Thid is a test: public class TempTest {
@Test
public void test(){
class GooglePage {
private WebDriver driver;
@FindBy(name = ".gbqfif.WRONG")
WebElement myWebElement;
GooglePage(WebDriver driver) {
this.driver = driver;
driver.manage().timeouts().implicitlyWait(2, TimeUnit.SECONDS);
HtmlElementLoader.populate(this, driver);
driver.get("http://google.com");
}
}
WebDriver driver = new FirefoxDriver();
GooglePage page = new GooglePage(driver);
try {
page.myWebElement.isDisplayed();
} catch (NoSuchElementException e) {
System.out.println("\n*** With Firefox driver ***\n\n\n");
e.printStackTrace();
}
System.setProperty("webdriver.chrome.driver", "webdriver-webui-tests/webdriver/" + System.getProperty("os.name") + "/chromedriver");
driver = new ChromeDriver();
page = new GooglePage(driver);
try {
page.myWebElement.isDisplayed();
} catch (NoSuchElementException e) {
System.out.println("\n\n*** With Chrome driver (latest version) ***\n\n");
e.printStackTrace();
}
driver.quit();
}
} And this is console output:
As you can see there is a locator info in case of Firefox driver:
And there is no any info in case of Chrome driver:
In both cases I did not find any 'pretty print' of the name of my element. I would expect something like: 'My Web Element' what is the output of the myWebElement.toString(); Maybe something is wrong with my environment... It would be great if you try to reproduce the issue on your env. |
just encountered this myself. the lack of locator info using the chrome driver was sufficient motivation to download firefox and switch my testing there. chrome driver: firefox driver: |
What you actually posted looks like log of Selenium server. It's up to Selenium server whatever or not to display used selector. What @artkoshelev meant is that in your Java test suite you'll get nice exception back (when element location failed by given Selenium server isn't displaying any test suite exception except ones that it generates by itself. |
you're right. |
Will be added cause info with selector in #75. Can i close this issue for now? @olionajudah |
If the element could not be find by locator (in the chromedriver case at least), The following exception is thrown:
Why not add additional info about the element (annotated with @findby) which was not find? At least something to identify it from the test result... The variable name, locator (xpath, css used in FindBy, etc.), etc...
The text was updated successfully, but these errors were encountered: