testdevlab / TestUI

MIT License
33 stars 9 forks source link

Add method to retrieve given UIElement's selector #89

Closed Kirils-Podgalskis closed 1 year ago

Kirils-Podgalskis commented 1 year ago

UIElement is defined by CSS selector, but there is no way to retrieve it's selector

alvarolaserna commented 1 year ago

Can you explain this further? I dont fully understand the issue, and if you can put some example, that would be helpful

Kirils-Podgalskis commented 1 year ago

for example, I declare UIElement as following: private final UIElement body = E(byCssSelector(".body"));

but there is no method to return body's CSS selector back. This method would be helpful when working directly with Selenium or JSExecutor, because they do not support UIElement. Example:

private final UIElement text_1 = E(byCssSelector("#text_field_1"));
private final UIElement text_2 = E(byCssSelector("#text_field_2"));

--//-- Let's assume I want to execute JS script. Because I am unable to retrieve UIElement's selector or id and therefore pass its value to variable, I have to reenter given element's CSS selector(which is not an adequate approach when there are a lot of elements):

((JavascriptExecutor) WebDriverRunner.getWebDriver()).executeScript(
        'document.querySelector("#text_field_1").contains("text")'
);

((JavascriptExecutor) WebDriverRunner.getWebDriver()).executeScript(
        'document.querySelector("#text_field_2").contains("text")'
);

instead of this:

UIElement[] array = {text_1, text_2}
for (int i=0; i<array.length; i++) 
{ 
    ((JavascriptExecutor) WebDriverRunner.getWebDriver()).executeScript(
        'document.querySelector(' + array[i].getCSS() + ').contains("text")'
    );
}
alvarolaserna commented 1 year ago

you can use this, remember that you can add arguments to the javaScript command (https://www.guru99.com/execute-javascript-selenium-webdriver.html):

UIElement[] array = {text_1, text_2}
for (int i=0; i<array.length; i++) 
{ 
    ((JavascriptExecutor) WebDriverRunner.getWebDriver()).executeScript(
        "document.querySelector(arguments[0]).contains(\"text\")", array[i].getSelenideElement().getWrappedElement()
    );
}