Skip to content
This repository was archived by the owner on Feb 2, 2025. It is now read-only.

Commit aaf73e6

Browse files
authored
Merge pull request #78 from ractoc/develop
Added setFocusToElement to replace the Focus keyword
2 parents 831945f + aaba73b commit aaf73e6

File tree

1 file changed

+51
-10
lines changed
  • src/main/java/com/github/markusbernhardt/seleniumlibrary/keywords

1 file changed

+51
-10
lines changed

src/main/java/com/github/markusbernhardt/seleniumlibrary/keywords/Element.java

Lines changed: 51 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -247,8 +247,8 @@ public void assignIdToElement(String locator, String id) {
247247
.executeScript(String.format("arguments[0].id = '%s';", id), elements.get(0));
248248
}
249249

250-
@RobotKeyword("Verify the element identified by ``locator`` is found on the current page\r\n" +
251-
"\r\n" +
250+
@RobotKeyword("Verify the element identified by ``locator`` is enabled\r\n" +
251+
"\r\n" +
252252
"Key attributes for arbitrary elements are id and name. See `Introduction` for details about log levels and locators.")
253253
@ArgumentNames({ "locator" })
254254
public void elementShouldBeEnabled(String locator) {
@@ -257,7 +257,7 @@ public void elementShouldBeEnabled(String locator) {
257257
}
258258
}
259259

260-
@RobotKeyword("Verify the element identified by ``locator`` is disabled.\r\n" +
260+
@RobotKeyword("Verify the element identified by ``locator`` is disabled.\r\n" +
261261
"\r\n" +
262262
"Key attributes for arbitrary elements are id and name. See `Introduction` for details about locators.")
263263
@ArgumentNames({ "locator" })
@@ -267,6 +267,16 @@ public void elementShouldBeDisabled(String locator) {
267267
}
268268
}
269269

270+
@RobotKeyword("Verify the element identified by ``locator`` is focused\r\n" +
271+
"\r\n" +
272+
"Key attributes for arbitrary elements are id and name. See `Introduction` for details about log levels and locators.")
273+
@ArgumentNames({ "locator" })
274+
public void elementShouldBeFocused(String locator) {
275+
if (!isFocused(locator)) {
276+
throw new SeleniumLibraryNonFatalException(String.format("Element %s is not focused.", locator));
277+
}
278+
}
279+
270280
@RobotKeyword("Verify the element identified by ``locator`` is selected.\r\n" +
271281
"\r\n" +
272282
"Key attributes for arbitrary elements are id and name. See `Introduction` for details about locators.")
@@ -419,18 +429,32 @@ public void elementTextShouldNotBe(String locator, String text, String...params)
419429
"\r\n" +
420430
"The ``attribute_locator`` consists of element locator followed by an @ sign and attribute name. Example: element_id@class\r\n" +
421431
"\r\n" +
422-
"Key attributes for arbitrary elements are id and name. See `Introduction` for details about locators.")
432+
"Key attributes for arbitrary elements are id and name. See `Introduction` for details about locators.\r\n" +
433+
"\r\n" +
434+
"Passing attribute name as part of the locator was removed in SeleniumLibrary 3.2. The explicit attribute argument should be used instead.")
423435
@ArgumentNames({ "attributeLocator" })
436+
@Deprecated
424437
public String getElementAttribute(String attributeLocator) {
425438
String[] parts = parseAttributeLocator(attributeLocator);
439+
return getElementAttribute(parts[0], parts[1]);
440+
}
441+
442+
@RobotKeyword("Returns value of attribute from element locator.\r\n" +
443+
"\r\n" +
444+
"See the `Locating elements` section for details about the locator syntax.\r\n" +
445+
"\r\n" +
446+
"Example: ${id}= Get Element Attribute css:h1 id\r\n" +
447+
"\r\n" +
448+
"Passing attribute name as part of the locator was removed in SeleniumLibrary 3.2. The explicit attribute argument should be used instead.")
449+
@ArgumentNames({ "locator", "attribute" })
450+
public String getElementAttribute(String locator, String attribute) {
426451

427-
List<WebElement> elements = elementFind(parts[0], true, false);
452+
List<WebElement> elements = elementFind(locator, true, false);
428453

429454
if (elements.size() == 0) {
430-
throw new SeleniumLibraryNonFatalException(String.format("Element '%s' not found.", parts[0]));
455+
throw new SeleniumLibraryNonFatalException(String.format("Element '%s' not found.", locator));
431456
}
432-
433-
return elements.get(0).getAttribute(parts[1]);
457+
return elements.get(0).getAttribute(attribute);
434458
}
435459

436460
@RobotKeyword("Clears the text from element identified by ``locator``.\r\n" +
@@ -582,11 +606,20 @@ public void doubleClickElement(String locator) {
582606
action.doubleClick(elements.get(0)).perform();
583607
}
584608

585-
@RobotKeyword("Set the focus to the element identified by ``locator``.\r\n" +
586-
"\r\n" +
609+
@RobotKeyword("Set the focus to the element identified by ``locator``.\r\n" +
610+
"\r\n" +
587611
"Key attributes for arbitrary elements are id and name. See `Introduction` for details about locators.")
588612
@ArgumentNames({ "locator" })
613+
@Deprecated
589614
public void focus(String locator) {
615+
setFocusToElement(locator);
616+
}
617+
618+
@RobotKeyword("Set the focus to the element identified by ``locator``.\r\n" +
619+
"\r\n" +
620+
"Key attributes for arbitrary elements are id and name. See `Introduction` for details about locators.")
621+
@ArgumentNames({ "locator" })
622+
public void setFocusToElement(String locator) {
590623
List<WebElement> elements = elementFind(locator, true, true);
591624
((JavascriptExecutor) browserManagement.getCurrentWebDriver()).executeScript("arguments[0].focus();",
592625
elements.get(0));
@@ -977,6 +1010,14 @@ protected boolean isEnabled(String locator) {
9771010
return true;
9781011
}
9791012

1013+
protected boolean isFocused(String locator) {
1014+
List<WebElement> elements = elementFind(locator, true, true);
1015+
WebElement element = elements.get(0);
1016+
WebDriver current = browserManagement.getCurrentWebDriver();
1017+
WebElement focused = current.switchTo().activeElement();
1018+
return element.equals(focused);
1019+
}
1020+
9801021
protected boolean isVisible(String locator) {
9811022
List<WebElement> elements = elementFind(locator, true, false);
9821023
if (elements.size() == 0) {

0 commit comments

Comments
 (0)