本文整理汇总了Java中org.openqa.selenium.JavascriptExecutor类的典型用法代码示例。如果您正苦于以下问题:Java JavascriptExecutor类的具体用法?Java JavascriptExecutor怎么用?Java JavascriptExecutor使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
JavascriptExecutor类属于org.openqa.selenium包,在下文中一共展示了JavascriptExecutor类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: doClick
import org.openqa.selenium.JavascriptExecutor; //导入依赖的package包/类
private void doClick() {
try {
new RealHtmlElementState(this).waitToBecomeExisting();
WebElement element = RealHtmlElementLocator.findElement(this);
try {
element.click();
} catch (ElementNotVisibleException enve) {
if (!UiEngineConfigurator.getInstance().isWorkWithInvisibleElements()) {
throw enve;
}
((JavascriptExecutor) webDriver).executeScript("arguments[0].click()", element);
}
} catch (Exception e) {
((AbstractRealBrowserDriver) super.getUiDriver()).clearExpectedPopups();
throw new SeleniumOperationException(this, "click", e);
}
UiEngineUtilities.sleep();
((AbstractRealBrowserDriver) super.getUiDriver()).handleExpectedPopups();
}
示例2: clearByJS
import org.openqa.selenium.JavascriptExecutor; //导入依赖的package包/类
@Action(object = ObjectType.SELENIUM, desc = "Clear the element [<Object>]")
public void clearByJS() {
if (elementPresent()) {
try {
JavascriptExecutor js = (JavascriptExecutor) Driver;
js.executeScript("arguments[0].value=''", Element);
Report.updateTestLog(Action, "Cleared value from '" + ObjectName + "'", Status.DONE);
} catch (Exception ex) {
Logger.getLogger(JSCommands.class.getName()).log(Level.SEVERE, null, ex);
Report.updateTestLog(Action,
"Couldn't clear value on " + ObjectName + " - Exception " + ex.getMessage(), Status.FAIL);
}
} else {
throw new ElementException(ElementException.ExceptionType.Element_Not_Found, ObjectName);
}
}
示例3: selectByJS
import org.openqa.selenium.JavascriptExecutor; //导入依赖的package包/类
@Action(object = ObjectType.SELENIUM, desc = "Select element [<Object>] ", input = InputType.YES)
public void selectByJS() {
if (elementPresent()) {
try {
JavascriptExecutor js = (JavascriptExecutor) Driver;
Object value = js.executeScript(
"var options=arguments[0].getElementsByTagName('option');" + "for(var i=0;i<options.length;i++)"
+ "{" + "var value=options[i].textContent?options[i].textContent:options[i].innerText;"
+ "if(value.trim()==='" + Data.trim() + "')" + "{"
+ "options[i].setAttribute('selected','selected');" + "return true;" + "}" + "}"
+ "return false;",
Element);
if (value != null && value.toString().trim().toLowerCase().equals("true")) {
Report.updateTestLog(Action, "Item " + Data + " is selected from" + ObjectName, Status.DONE);
} else {
Report.updateTestLog(Action, "Item " + Data + " is not available in the" + ObjectName, Status.FAIL);
}
} catch (Exception ex) {
Logger.getLogger(JSCommands.class.getName()).log(Level.SEVERE, null, ex);
Report.updateTestLog(Action,
"Couldn't select value from " + ObjectName + " - Exception " + ex.getMessage(), Status.FAIL);
}
} else {
throw new ElementException(ElementException.ExceptionType.Element_Not_Found, ObjectName);
}
}
示例4: executeScript
import org.openqa.selenium.JavascriptExecutor; //导入依赖的package包/类
public void executeScript() throws Throwable {
try {
driver = new JavaDriver();
SwingUtilities.invokeAndWait(new Runnable() {
@Override public void run() {
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
WebElement element1 = driver.findElement(By.name("text-field"));
WebElement element2 = (WebElement) ((JavascriptExecutor) driver).executeScript("return $1;", element1);
AssertJUnit.assertEquals(element1, element2);
} finally {
JavaElementFactory.reset();
}
}
示例5: executeScriptWithPrimitiveReturn
import org.openqa.selenium.JavascriptExecutor; //导入依赖的package包/类
public void executeScriptWithPrimitiveReturn() throws Throwable {
try {
driver = new JavaDriver();
SwingUtilities.invokeAndWait(new Runnable() {
@Override public void run() {
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
WebElement element1 = driver.findElement(By.name("text-field"));
Number width = (Number) ((JavascriptExecutor) driver).executeScript("return Double.valueOf($1.getSize().getWidth());",
element1);
AssertJUnit.assertTrue(width.doubleValue() > 0);
} finally {
JavaElementFactory.reset();
}
}
示例6: executeAsyncScript
import org.openqa.selenium.JavascriptExecutor; //导入依赖的package包/类
public void executeAsyncScript() throws Throwable {
try {
driver = new JavaDriver();
SwingUtilities.invokeAndWait(new Runnable() {
@Override public void run() {
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
WebElement element1 = driver.findElement(By.name("text-field"));
WebElement element2 = (WebElement) ((JavascriptExecutor) driver).executeAsyncScript("$2.call($1);", element1);
AssertJUnit.assertEquals(element1, element2);
} finally {
JavaElementFactory.reset();
}
}
示例7: executeAsyncScriptWithoutCallback
import org.openqa.selenium.JavascriptExecutor; //导入依赖的package包/类
public void executeAsyncScriptWithoutCallback() throws Throwable {
try {
driver = new JavaDriver();
SwingUtilities.invokeAndWait(new Runnable() {
@Override public void run() {
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
WebElement element1 = driver.findElement(By.name("text-field"));
((JavascriptExecutor) driver).executeAsyncScript("$1.setText(\"hello world\");", element1);
throw new MissingException(WebDriverException.class);
} catch (WebDriverException e) {
} finally {
JavaElementFactory.reset();
}
}
示例8: executeAsyncScriptWithNullReturn
import org.openqa.selenium.JavascriptExecutor; //导入依赖的package包/类
public void executeAsyncScriptWithNullReturn() throws Throwable {
try {
driver = new JavaDriver();
SwingUtilities.invokeAndWait(new Runnable() {
@Override public void run() {
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
WebElement element1 = driver.findElement(By.name("text-field"));
WebElement element2 = (WebElement) ((JavascriptExecutor) driver).executeAsyncScript("$2.call(null);", element1);
AssertJUnit.assertNull(element2);
} finally {
JavaElementFactory.reset();
}
}
示例9: getCourseListings
import org.openqa.selenium.JavascriptExecutor; //导入依赖的package包/类
@Override
public void getCourseListings() throws MalformedURLException {
// From the home page, retrieve all links to current courses
List<WebElement> links = action.driver.findElements(By.xpath("//*[@data-pats='classes_dropdown_item']"));
System.out.println(links.size());//*[@id="network_j6feqds1z771k1"]/
//*[@id="network_j6feqds1z771k1"]/div
CourseListing[] cls = new CourseListing[links.size()];
for (int i = 0; i < links.size(); i++) {
cls[i] = new CourseListing();
String script = "return arguments[0].innerText";
cls[i].course_name = (String) ((JavascriptExecutor) action.driver).executeScript(script, links.get(i));
String course_url = baseurl + "/class/" + links.get(i).getAttribute("id").replace("network_", "");
cls[i].base_url = course_url;
System.out.println(cls[i].base_url + "**************");
}
addSubGrabber(cls);
}
示例10: scrollTo
import org.openqa.selenium.JavascriptExecutor; //导入依赖的package包/类
/**
* Scroll the webpage.
*
* @param topBottom possible values: top, bottom
* @param leftRight possible values: left, right
* @returns {Promise<any>}
*/
@When("^scroll \"([^\"]*)\" \"([^\"]*)\"$")
public void scrollTo(String topBottom, String leftRight) {
WebDriver driver = WebDriverRunner.getWebDriver();
JavascriptExecutor jse = (JavascriptExecutor) driver;
int x = 0;
int y = 0;
Long width = (Long) jse.executeScript("return $(document).width()");
Long height = (Long) jse.executeScript("return $(document).height()");
if (leftRight.equals("right")) {
y = width.intValue();
}
if (topBottom.equals("bottom")) {
x = height.intValue();
}
jse.executeScript("(browserX, browserY) => window.scrollTo(browserX, browserY)", x, y);
}
示例11: updateText
import org.openqa.selenium.JavascriptExecutor; //导入依赖的package包/类
/**
* Update a html input text with a text.
*
* @param pageElement
* Is target element
* @param textOrKey
* Is the new data (text or text in context (after a save))
* @param keysToSend
* character to send to the element after {@link org.openqa.selenium.WebElement#sendKeys(CharSequence...) sendKeys} with textOrKey
* @param args
* list of arguments to format the found selector with
* @throws TechnicalException
* is thrown if you have a technical error (format, configuration, datas, ...) in NoraUi.
* Exception with {@value com.github.noraui.utils.Messages#FAIL_MESSAGE_ERROR_ON_INPUT} message (with screenshot, no exception)
* @throws FailureException
* if the scenario encounters a functional error
*/
protected void updateText(PageElement pageElement, String textOrKey, CharSequence keysToSend, Object... args) throws TechnicalException, FailureException {
String value = Context.getValue(textOrKey) != null ? Context.getValue(textOrKey) : textOrKey;
if (!"".equals(value)) {
try {
WebElement element = Context.waitUntil(ExpectedConditions.elementToBeClickable(Utilities.getLocator(pageElement, args)));
element.clear();
if (DriverFactory.IE.equals(Context.getBrowser())) {
String javascript = "arguments[0].value='" + value + "';";
((JavascriptExecutor) getDriver()).executeScript(javascript, element);
} else {
element.sendKeys(value);
}
if (keysToSend != null) {
element.sendKeys(keysToSend);
}
} catch (Exception e) {
new Result.Failure<>(e.getMessage(), Messages.format(Messages.getMessage(Messages.FAIL_MESSAGE_ERROR_ON_INPUT), pageElement, pageElement.getPage().getApplication()), true,
pageElement.getPage().getCallBack());
}
} else {
logger.debug("Empty data provided. No need to update text. If you want clear data, you need use: \"I clear text in ...\"");
}
}
示例12: setFieldValue
import org.openqa.selenium.JavascriptExecutor; //导入依赖的package包/类
/**
* Set value of specified table field
*
* @param value the new table cell value
* @param row the field row starting at 0
* @param column the field column starting at 0
*/
@Override
@PublicAtsApi
public void setFieldValue( String value, int row, int column ) {
new HiddenHtmlElementState(this).waitToBecomeExisting();
WebElement table = HiddenHtmlElementLocator.findElement(this);
String script = "var table = arguments[0]; var row = arguments[1]; var col = arguments[2];"
+ "if (row > table.rows.length) { return \"Cannot access row \" + row + \" - table has \" + table.rows.length + \" rows\"; }"
+ "if (col > table.rows[row].cells.length) { return \"Cannot access column \" + col + \" - table row has \" + table.rows[row].cells.length + \" columns\"; }"
+ "table.rows[row].cells[col].textContent = '" + value + "';";
((JavascriptExecutor) webDriver).executeScript(script, table, row, column);
}
示例13: getFieldValue
import org.openqa.selenium.JavascriptExecutor; //导入依赖的package包/类
/**
* Get the value of the specified table field
*
* @param row the field row starting at 0
* @param column the field column starting at 0
* @return the value
*/
@Override
@PublicAtsApi
public String getFieldValue( int row, int column ) {
new HiddenHtmlElementState(this).waitToBecomeExisting();
WebElement table = HiddenHtmlElementLocator.findElement(this);
String script = "var table = arguments[0]; var row = arguments[1]; var col = arguments[2];"
+ "if (row > table.rows.length) { return \"Cannot access row \" + row + \" - table has \" + table.rows.length + \" rows\"; }"
+ "if (col > table.rows[row].cells.length) { return \"Cannot access column \" + col + \" - table row has \" + table.rows[row].cells.length + \" columns\"; }"
+ "return table.rows[row].cells[col];";
JavascriptExecutor jsExecutor = (JavascriptExecutor) webDriver;
Object value = jsExecutor.executeScript(script, table, row, column);
if (value instanceof WebElement) {
return ((WebElement) value).getText().trim();
}
return null;
}
示例14: getFieldValue
import org.openqa.selenium.JavascriptExecutor; //导入依赖的package包/类
/**
* Get the value of the specified table field
*
* @param row the field row starting at 0
* @param column the field column starting at 0
* @return
*/
@Override
@PublicAtsApi
public String getFieldValue( int row, int column ) {
new RealHtmlElementState(this).waitToBecomeExisting();
WebElement table = RealHtmlElementLocator.findElement(this);
String script = "var table = arguments[0]; var row = arguments[1]; var col = arguments[2];"
+ "if (row > table.rows.length) { return \"Cannot access row \" + row + \" - table has \" + table.rows.length + \" rows\"; }"
+ "if (col > table.rows[row].cells.length) { return \"Cannot access column \" + col + \" - table row has \" + table.rows[row].cells.length + \" columns\"; }"
+ "return table.rows[row].cells[col];";
Object value = ((JavascriptExecutor) webDriver).executeScript(script, table, row, column);
if (value instanceof WebElement) {
return ((WebElement) value).getText().trim();
}
return null;
}
示例15: click
import org.openqa.selenium.JavascriptExecutor; //导入依赖的package包/类
/**
* Simulate mouse click action
*/
@Override
@PublicAtsApi
public void click() {
new RealHtmlElementState(this).waitToBecomeExisting();
try {
WebElement element = RealHtmlElementLocator.findElement(this);
try {
element.click();
} catch (ElementNotInteractableException enie) {
if (!UiEngineConfigurator.getInstance().isWorkWithInvisibleElements()) {
throw enie;
}
((JavascriptExecutor) webDriver).executeScript("arguments[0].click()", element);
}
} catch (Exception e) {
throw new SeleniumOperationException(this, "click", e);
}
}