selenium.JavascriptExecutor.executeScript() 使用实例

 
Example 1
Project: syndesis-qe   File: CommonSteps.java   View Source Code	Vote up	6 votes
/**
 * Scroll the webpage.
 *
 * @param topBottom possible values: top, bottom
 * @param leftRight possible values: left, right
 * @returns {Promise}
 */
@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);
}
 
Example 2
Project: ats-framework   File: HiddenHtmlTable.java   View Source Code	Vote up	6 votes
/**
 * 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;
}
 
Example 3
Project: Cognizant-Intelligent-Test-Scripter   File: JSCommands.java   View Source Code	Vote up	6 votes
@Action(object = ObjectType.SELENIUM, desc = "Set encrypted data on []", 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);
    }
}
 
Example 4
Project: Cognizant-Intelligent-Test-Scripter   File: JSCommands.java   View Source Code	Vote up	6 votes
@Action(object = ObjectType.SELENIUM, desc = "Click on []")
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);
    }
}
 
Example 5
Project: opentest   File: ExecuteScript.java   View Source Code	Vote up	6 votes
@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);
    }
}
 
Example 6
Project: Cognizant-Intelligent-Test-Scripter   File: JSCommands.java   View Source Code	Vote up	6 votes
@Action(object = ObjectType.SELENIUM, desc = "Clear the element []")
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);
    }
}
 
Example 7
Project: phoenix.webui.framework   File: SeleniumEngine.java   View Source Code	Vote up	5 votes
/**
 * 计算工具栏高度
 * @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;
}
 
Example 8
Project: Cognizant-Intelligent-Test-Scripter   File: ImageCommand.java   View Source Code	Vote up	5 votes
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));
    }
}
 
Example 9
Project: AlipayAuto   File: AlipayAuto.java   View Source Code	Vote up	5 votes
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 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��д