我们有个日志发表功能,几乎所有的网站都有,我们网站日志的编辑框是一个内嵌的iframe,用selenium录制脚本的时候,发现检索不到,导致日志根本没有办法提交,下图是我们网址日志的截图
之后,找到了selenium.selectFrame()方法,但是网站的"发表"按钮并不在iframe里面,所有就一直找不到“发表”按钮,测试通不过啊,终于上网找啊找,终于找到一个方法,其实很简单,但是由于对selenium的方法还不熟悉,所以还是花了不少时间的。
代码如下:
package com.jiaoyu;
import com.thoughtworks.selenium.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.util.regex.Pattern;
public class Iframe extends SeleneseTestCase {
@Before
public void setUp() throws Exception {
selenium = new DefaultSelenium("localhost", 4444, "*chrome", "http://jiaxiaoquan.staff.139.com/");
selenium.start();
}
@Test
public void testIframe() throws Exception {
selenium.open("/webroot/apps/blog/index.php?s=/Index/addBlog");
this.login();
selenium.type("title", "测试iframe");
selenium.selectFrame("//iframe[@class='ke-iframe']");
selenium.type("//body[@class='ke-content']", "一二三四五六七八九十一二三四五六七八九十一二三四五六七八九十");
selenium.selectFrame("relative=top"); //关键在于这一句了 selenium.selectFrame("relative=up");
selenium.click("//input[@value='发 表']");
selenium.waitForPageToLoad("30000");
}
public void login(){
String username = "13948895822";
String passwd = "111111";
selenium.type("mobile", username);
selenium.type("passwd", passwd);
selenium.click("//input[@value='登 录']");
selenium.waitForPageToLoad("30000");
}
@After
public void tearDown() throws Exception {
selenium.stop();
}
}
这篇文章对selenium.selectFrame()方法讲的很清晰:http://www.testingexcellence.com/accessing-iframe-data-with-selenium-rc/
IFrame: By Name: selenium.selectFrame(“mainFrame”); //name of the IFrame
By DOM: selenium.selectFrame(“dom=window.frames[1]“); //second IFrame By Index:selenium.selectFrame(“index=1″); //second IFrame