WebDriver 中使用 Javascript

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!");
    }

}

posted @ 2012-11-26 13:19  allenbackpacker  阅读(2045)  评论(0编辑  收藏  举报