您的位置:首页 > Web前端 > JavaScript

WebDriver 中使用 Javascript

2012-11-26 13:19 411 查看
Use Javascript in WebDriver

package com.example.tests;

import java.io.File;
import java.util.regex.Pattern;
import java.util.concurrent.TimeUnit;
import org.junit.*;
import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.*;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.support.ui.Select;

public class BenbriaSele01 {
private WebDriver driver;
private String baseUrl;
private StringBuffer verificationErrors = new StringBuffer();
@Before
public void setUp() throws Exception {

//Use IE driver
File file = new File("E:/SW/Selenium/IEDriverServer32.exe");
System.setProperty("webdriver.ie.driver", file.getAbsolutePath());
DesiredCapabilities capabilities = DesiredCapabilities.internetExplorer();
capabilities.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, true);
capabilities.setCapability("ignoreProtectedModeSettings",true);
driver = new InternetExplorerDriver(capabilities);

// //Use Firefox
// driver = new FirefoxDriver();

baseUrl = "http://10.1.3.12/";
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

}

@Test
public void testBenbriaSele01() throws Exception {
// driver.get(baseUrl + "/blazecast/login.jsf");
driver.get(baseUrl);
driver.findElement(By.id("username")).sendKeys("admin");
driver.findElement(By.id("password")).sendKeys("admin");
// driver.findElement(By.id("login_submit")).click();
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("document.getElementById(\"login_submit\").click()",true);
}

@After
public void tearDown() throws Exception {
// driver.quit();
// String verificationErrorString = verificationErrors.toString();
// if (!"".equals(verificationErrorString)) {
// fail(verificationErrorString);
// }
System.out.println("tearDown!");
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: