本文整理汇总了Java中org.openqa.selenium.JavascriptExecutor.executeScript方法的典型用法代码示例。如果您正苦于以下问题:Java JavascriptExecutor.executeScript方法的具体用法?Java JavascriptExecutor.executeScript怎么用?Java JavascriptExecutor.executeScript使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.openqa.selenium.JavascriptExecutor
的用法示例。
在下文中一共展示了JavascriptExecutor.executeScript方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: 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);
}
示例2: setEncryptedByJS
import org.openqa.selenium.JavascriptExecutor; //导入方法依赖的package包/类
@Action(object = ObjectType.SELENIUM, desc = "Set encrypted data on [<Object>]", input=InputType.YES)
public void setEncryptedByJS() {
if (Data != null && Data.matches(".* Enc")) {
if (elementEnabled()) {
try {
Data = Data.substring(0, Data.lastIndexOf(" Enc"));
byte[] valueDecoded = Base64.decodeBase64(Data);
JavascriptExecutor js = (JavascriptExecutor) Driver;
js.executeScript("arguments[0].value='" + new String(valueDecoded) + "'", Element);
Report.updateTestLog(Action, "Entered Text '" + Data + "' on '" + ObjectName + "'", Status.DONE);
} catch (Exception ex) {
Report.updateTestLog(Action, ex.getMessage(), Status.FAIL);
Logger.getLogger(JSCommands.class.getName()).log(Level.SEVERE, null, ex);
}
} else {
throw new ElementException(ElementException.ExceptionType.Element_Not_Enabled, ObjectName);
}
} else {
Report.updateTestLog(Action, "Data not encrypted '" + Data + "'", Status.DEBUG);
}
}
示例3: 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);
}
}
示例4: 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;
}
示例5: clickByJS
import org.openqa.selenium.JavascriptExecutor; //导入方法依赖的package包/类
@Action(object = ObjectType.SELENIUM, desc = "Click on [<Object>]")
public void clickByJS() {
if (elementPresent()) {
try {
JavascriptExecutor js = (JavascriptExecutor) Driver;
js.executeScript("arguments[0].click();", Element);
Report.updateTestLog(Action, "Clicked on " + ObjectName, Status.DONE);
} catch (Exception ex) {
Logger.getLogger(JSCommands.class.getName()).log(Level.SEVERE, null, ex);
Report.updateTestLog(Action, "Couldn't click on " + ObjectName + " - Exception " + ex.getMessage(),
Status.FAIL);
}
} else {
throw new ElementException(ElementException.ExceptionType.Element_Not_Found, ObjectName);
}
}
示例6: run
import org.openqa.selenium.JavascriptExecutor; //导入方法依赖的package包/类
@Override
public void run() {
super.run();
String script = this.readStringArgument("script");
Boolean async = this.readBooleanArgument("async", false);
Integer timeoutSec = this.readIntArgument("timeoutSec", 20);
this.waitForAsyncCallsToFinish();
driver.manage().timeouts().setScriptTimeout(timeoutSec, TimeUnit.SECONDS);
JavascriptExecutor jsExecutor = (JavascriptExecutor)driver;
if (async) {
jsExecutor.executeAsyncScript(script);
} else {
jsExecutor.executeScript(script);
}
}
示例7: computeToolbarHeight
import org.openqa.selenium.JavascriptExecutor; //导入方法依赖的package包/类
/**
* 计算工具栏高度
* @return 高度
*/
public int computeToolbarHeight()
{
JavascriptExecutor jsExe = (JavascriptExecutor) driver;
Object objectHeight = jsExe.executeScript("return window.outerHeight - window.innerHeight;");
if(objectHeight instanceof Long)
{
toolbarHeight = ((Long) objectHeight).intValue();
}
return toolbarHeight;
}
示例8: pageDownBrowser
import org.openqa.selenium.JavascriptExecutor; //导入方法依赖的package包/类
public void pageDownBrowser(int dh) {
if (isHeadless()) {
SCREEN.type(Key.PAGE_DOWN);
} else {
dh = Math.max(0, dh);
JavascriptExecutor jse = ((JavascriptExecutor) Driver);
jse.executeScript(String.format("window.scrollBy(0, window.innerHeight-%s)", dh));
}
}
示例9: getOppositeUser
import org.openqa.selenium.JavascriptExecutor; //导入方法依赖的package包/类
private static String getOppositeUser(String transactionNo) {
// ��ȡ�ؼ��ֶ�Ӧ��������
WebElement keywordInput = driver.findElement(By.id("J-keyword"));
keywordInput.clear();
keywordInput.sendKeys(transactionNo);
WebElement keywordSelect = driver.findElement(By.id("keyword"));
List<WebElement> options = keywordSelect.findElements(By.tagName("option"));
// until������ʾֱ���ɵ��ٵ�
// WebElement selectElement = wait.until(ExpectedConditions
// .visibilityOfElementLocated(By.id("keyword")));
// ��Ҫִ��JavaScript��䣬����ǿתdriver
JavascriptExecutor js = (JavascriptExecutor) driver;
// Ҳ������ô��setAttribute("style","");
js.executeScript("document.getElementById('keyword').style.display='list-item';");
js.executeScript("document.getElementById('keyword').removeAttribute('smartracker');");
js.executeScript("document.getElementById('keyword').options[1].selected = true;");
js.executeScript("document.getElementById('J-select-range').style.display='list-item';");
// ���ý���ʱ��ѡ��
Select selectTime = new Select(driver.findElement(By.id("J-select-range")));
selectTime.selectByIndex(3);// ѡ�е������������
System.out.println("selectTime.isMultiple() : " + selectTime.isMultiple());
// ���ùؼ���ѡ��
Select selectKeyword = new Select(driver.findElement(By.id("keyword")));
// selectKeyword.selectByValue("bizInNo");//�˴���value��д<option>��ǩ�е�valueֵ
selectKeyword.selectByIndex(1);// ѡ�е��ǽ���
System.out.println("selectKeyword.isMultiple() : " + selectKeyword.isMultiple());
WebElement queryButton = driver.findElement(By.id("J-set-query-form"));// �õ�������ť
// ���������ť
queryButton.submit();
WebElement tr = driver.findElement(By.id("J-item-1"));// �Ȼ�ȡtr
WebElement td = tr.findElement(By.xpath("//*[@id=\"J-item-1\"]/td[5]/p[1]"));
return td.getText();
}
示例10: html5Erreur
import org.openqa.selenium.JavascriptExecutor; //导入方法依赖的package包/类
@Override
public void html5Erreur(String selector) {
this.logger.debug("Appel du test messageErreur()");
JavascriptExecutor js = this;
Boolean s = (Boolean) js.executeScript("return document.querySelector('"
+ selector + "').validity.valid");
if (!s) {
Assert.fail(selector + " " + s
+ " Un message de sevérité ERROR devrait être présent!");
}
}
示例11: highlightElement
import org.openqa.selenium.JavascriptExecutor; //导入方法依赖的package包/类
private void highlightElement(
boolean disregardConfiguration ) {
if (webDriver instanceof PhantomJSDriver) {
// it is headless browser
return;
}
if (disregardConfiguration || UiEngineConfigurator.getInstance().getHighlightElements()) {
try {
WebElement webElement = RealHtmlElementLocator.findElement(element);
String styleAttrValue = webElement.getAttribute("style");
JavascriptExecutor js = (JavascriptExecutor) webDriver;
js.executeScript("arguments[0].setAttribute('style', arguments[1]);",
webElement,
"background-color: #ff9; border: 1px solid yellow; box-shadow: 0px 0px 10px #fa0;"); // to change text use: "color: yellow; text-shadow: 0 0 2px #f00;"
Thread.sleep(500);
js.executeScript("arguments[0].setAttribute('style', arguments[1]);",
webElement,
styleAttrValue);
} catch (Exception e) {
// swallow this error as highlighting is not critical
}
}
}
示例12: scrollTo
import org.openqa.selenium.JavascriptExecutor; //导入方法依赖的package包/类
/**
* This method makes the driver scroll to the specified webelement in
* browser
**/
public static boolean scrollTo(WebElement wb, WebDriver driver) {
try {
JavascriptExecutor je = (JavascriptExecutor) driver;
je.executeScript("arguments[0].scrollIntoView(true);", wb);
} catch (Exception e) {
e.printStackTrace();
}
return wb.isDisplayed();
}
示例13: addUserFunction
import org.openqa.selenium.JavascriptExecutor; //导入方法依赖的package包/类
public void addUserFunction(String userName, String passwordText){
user.sendKeys(userName);
password.sendKeys(passwordText);
loginButton.click();
waitForElement(userManagement, 30);
userManagement.sendKeys(Keys.ENTER);
JavascriptExecutor ex = (JavascriptExecutor)driver;
ex.executeScript("arguments[0].click();", addUser);
waitForElement(userType, 60);
}
示例14: createAndSwitchToWindow
import org.openqa.selenium.JavascriptExecutor; //导入方法依赖的package包/类
@Action(object = ObjectType.BROWSER, desc ="Open a new Browser window", input =InputType.OPTIONAL)
public void createAndSwitchToWindow() {
try {
JavascriptExecutor js = (JavascriptExecutor) Driver;
js.executeScript("window.open(arguments[0])", Data);
Set<String> Handles = Driver.getWindowHandles();
Driver.switchTo().window((String) Handles.toArray()[Handles.size() - 1]);
Report.updateTestLog(Action, "New Window Created and Switched to that ", Status.DONE);
} catch (Exception ex) {
Logger.getLogger(this.getClass().getName()).log(Level.OFF, null, ex);
Report.updateTestLog(Action, "Error in Switching Window -" + ex.getMessage(), Status.DEBUG);
}
}
示例15: execute
import org.openqa.selenium.JavascriptExecutor; //导入方法依赖的package包/类
private String execute(final String command, final Object... args) {
this.driver.manage().timeouts().setScriptTimeout(30, TimeUnit.SECONDS);
JavascriptExecutor js = ((JavascriptExecutor) this.driver);
Object response = js.executeAsyncScript(command, args);
String result = (String) js.executeScript("return JSON.stringify(arguments[0]);", response);
return result;
}