Custom Locators

Hey everyone,

I’m trying to figure out how to use custom locators. It seems to work for locating elements, but if I use a keyword such as Wait Until Page Contains Element, it fails immediately without waiting for the requested timeout.

My custom locator is:
Get Element By Data-TestID
[Arguments] ${browser} ${locator} ${tag} ${constraints}
${e} Get WebElement //*[@data-testid="${locator}"]
[Return] ${e}

And I’m using it like this:
Test Setup Add Location Strategy data-testid Get Element By Data-TestID

1 Like

Hard to say anything concrete with these details. Could you show the relevant parts where you create the custom selector, use it and snippets of a log with --loglevel trace set from the command line.

Here’s my code:

*** Settings ***
Test Setup       Add Location Strategy  data-testid  Test Custom Locator

*** Keywords ***

Test Custom Locator
	[Arguments]  ${browser}  ${locator}  ${tag}  ${constraints}
	${e}  Get WebElement  //*[@data-testid="${locator}"]	
	[Return]  ${e} 	


*** Test Cases *** 
   
Temp 
	Start Browser  ${PELAdminURL}
	set log level  trace

	Wait Until Page Contains Element   //*[@data-testid="45AEE8C1-63D4-4AFD-85B0-BB83FF8170ED"]   #this works  
	Click Element                            data-testid:45AEE8C1-63D4-4AFD-85B0-BB83FF8170ED     #this works
	Wait Until Page Contains Element   data-testid:testtesttest   timeout=90                      #this should fail after 90 seconds

Here’s the log showing the keyword not waiting:

This could work:

Test Custom Locator
	[Arguments]  ${browser}  ${locator}  ${tag}  ${constraints}
	${e}  Get WebElements  //*[@data-testid="${locator}"]	
	[Return]  ${e}

But there might be otter problems with assert keywords.

Instead of using the custom locator, you could build easily this as plugin.

1 Like

No luck :frowning_face:

I’m thinking I’ll just use xpath every time instead of messing with plugins, might be a bit overkill.

Thanks for your help!

Could you show the stack trace from log with the modified custom locator?

Also what version of SeleniumLibrary you are using?

Wait I don’t know what I did wrong when I tested your solution the first time, but it does work perfectly! Now I’m really curious why.

1 Like

#####################################################################
Please try to put Sleep function before Wait Until Page. Example:

Wait Until Page Contains Element //*[@data-testid=“45AEE8C1-63D4-4AFD-85B0-BB83FF8170ED”] #this works
Click Element data-testid:45AEE8C1-63D4-4AFD-85B0-BB83FF8170ED #this works
Sleep 1
Wait Until Page Contains Element data-testid:testtesttest timeout=90 #this should fail after 90 seconds