iframe
package com.goodhope.test;
public class Selenium extends FunctionalTestCase {
public void testFortest() {
selenium.open("http://127.0.0.1:1080/WebTours/");
selenium.type("username", "jojo");
selenium.type("password", "bean");
clickAndWait("login");
selenium.selectFrame("info");
assertTextPresent("Welcome, jojo");
selenium.selectFrame("relative=top"); //回到顶部,这样可以再选择frame,不然直接选择//frame会报错。
selenium.selectFrame("navbar");
clickAndWait("alt=Search Flights Button");//因为图片只有alt跟src两种定义,如果去掉alt=的话//会出错,因为selenium默认click动作,好像是id or name
selenium.selectFrame("info");
assertTextPresent("Find Flight");
clickAndWait("name=findFlights");
assertTextPresent("Flight departing from");
}
}
navigate
driver.navigate().refresh();
navigate():用来模拟浏览器工具栏上一些按钮的功能
Navigation: History and Location
Earlier, we covered navigating to a page using the “get” command ( driver.get("http://www.example.com")) As you’ve seen, WebDriver has a number of smaller, task-focused interfaces, and navigation is a useful task. Because loading a page is such a fundamental requirement, the method to do this lives on the main WebDriver interface, but it’s simply a synonym to:
driver.navigate().to("http://www.example.com");
To reiterate: “navigate().to()” and “get()” do exactly the same thing. One’s just a lot easier to type than the other!
The “navigate” interface also exposes the ability to move backwards and forwards in your browser’s history:
driver.navigate().forward(); driver.navigate().back();
Please be aware that this functionality depends entirely on the underlying browser. It’s just possible that something unexpected may happen when you call these methods if you’re used to the behaviour of one browser over another.
getAttribute
用getAttribute要得到属性如下:
selenium.getAttribute("//div[@id='mydiv']@style")
这里要得到id 为mydiv的层的style的属性
javascript
javascript:
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("CKEDITOR.instances['editor'].setData('想自由')");
可以用js直接去调用js方法
isElementPresent()
selenium之前的版本有:
selenium.isElementPresent()的方法
但是在selenium暂时还没有找到相应的方法,那可以去封装一个方法:
protected boolean IsElementPresent(By by) {
try {
driver.findElement(by);
return true;
} catch (Exception e) {
return false;
}
}
然后直接调用这个方法就OK了,比如:
assertTrue(IsElementPresent(By.id("fsfas")));
assertFalse(IsElementPresent(By.id("fsfas")));
Filling In Forms
We’ve already seen how to enter text into a textarea or text field, but what about the other elements? You can “toggle” the state of checkboxes, and you can use “setSelected” to set something like an OPTION tag selected. Dealing with SELECT tags isn’t too bad:
WebElement select = driver.findElement(By.xpath("//select"));
List<WebElement> allOptions = select.findElements(By.tagName("option"));
for (WebElement option : allOptions) {
System.out.println(String.format("Value is: %s", option.getValue()));
option.setSelected();
}
This will find the first “SELECT” element on the page, and cycle through each of it’s OPTIONs in turn, printing out their values, and selecting each in turn. As you can see, this isn’t the most efficient way of dealing with SELECT elements. WebDriver’s support classes include one called “Select”, which provides useful methods for interacting with these.
Select select = new Select(driver.findElement(By.xpath("//select")));
select.deselectAll();
select.selectByVisibleText("Edam");
This will deselect all OPTIONs from the first SELECT on the page, and then select the OPTION with the displayed text of “Edam”.
WebDriver之modifier key:Key Down / Up events only make s
执行如下代码:
Java代码 收藏代码
new Actions(driver).keyDown(Keys.CONTROL).keyDown(Keys.F5).keyUp(Keys.CONTROL).keyUp(Keys.F5).perform();
会报如下错误:
java.lang.IllegalArgumentException: Key Down / Up events only make sense for modifier keys.
Google 了一下Modifier Key:
Each key that appears on the keyboard without requiring modifiers are sent as a keydown followed by a key up.
If the server does not support native events and must simulate key strokes with JavaScript, it must generate keydown, keypress, and keyup events, in that order. The keypress event should only be fired when the corresponding key is for a printable character.
If a key requires a modifier key (e.g. "!" on a standard US keyboard), the sequence is: modifier down, key down, key up, modifier up, where key is the ideal unmodified key value (using the previous example, a "1").
Modifier keys (Ctrl, Shift, Alt, and Command/Meta) are assumed to be "sticky"; each modifier should be held down (e.g. only a keydown event) until either the modifier is encountered again in the sequence, or the NULL (U+E000) key is encountered.
Each key sequence is terminated with an implicit NULL key. Subsequently, all depressed modifier keys must be released (with corresponding keyup events) at the end of the sequence.
所以CTRL属于Modifier Key,需要这样写: Java代码 收藏代码
Actions actionObject = new Actions(driver);
actionObject.keyDown(Keys.CONTROL).sendKeys(Keys.F5).keyUp(Keys.CONTROL).perform();
运行测试通过,页面被成功强制刷新
webdriver to selenium
The Java version of WebDriver provides an implementation of the Selenium RC API. It is used like so:
// You may use any WebDriver implementation. Firefox is used here as an example
WebDriver driver = new FirefoxDriver();
// A "base url", used by selenium to resolve relative URLs
String baseUrl = "http://www.google.com";
// Create the Selenium implementation
Selenium selenium = new WebDriverBackedSelenium(driver, baseUrl);
// Perform actions with selenium
selenium.open("http://www.google.com");
selenium.type("name=q", "cheese");
selenium.click("name=btnG");
// Get the underlying WebDriver implementation back. This will refer to the
// same WebDriver instance as the "driver" variable above.
WebDriver driverInstance = ((WebDriverBackedSelenium) selenium).getUnderlyingWebDriver();
//Finally, close the browser. Call stop on the WebDriverBackedSelenium instance
//instead of calling driver.quit(). Otherwise, the JVM will continue running after
//the browser has been closed.
selenium.stop();