iFrame 定位的思想是先定位到iFrame,再定位iFrame里面的元素。
常用到的3個方法:
select()--------------------------選取要操作的frame
selectParentContainer() ------這個是回到上一層frame的
selectTopPage() ----------------這個是回到頂部頁面
具體例子:
1.新建2個html 頁面,放于D盤,作為被測試的頁面
main.html
- <html>
- <head >
- <title> FrameTest</title >
- </head >
- <body >
- <div id = "id1"> this is a div !</ div>
- <iframe id = "frame" frameborder="0" scrolling="no" style="left :0; position:absolute;" src = "iframe.html"></ iframe>
- </body >
- </html><span style="font-family: Tahoma;"> </span>
iframe.html
- <html>
- <head >
- <title> this is a frame!</title >
- </head >
- <body >
- <div id = "div1"> this is a div !oo! </div>
- <label> input:</label >
- <input id = "input1"></ input>
- </body >
- </html>
2.新建一個Page類,例子類目為P.java,代碼如下:
- import com.holmos.webtest.element.TextField;
- import com.holmos.webtest.struct.Frame;
- import com.holmos.webtest.struct.Page;
-
- public class P extends Page {
- public P() {
- super();
- this.comment = "iframe頁面";
- this.init();
- }
- // 先定位Frame
- public IFrameTest frame = new IFrameTest( "iframe");
- {
- frame.addIDlocator("frame" );// frame定位用ID定位
- }
- // 定位Frame里面的元素
- public class IFrameTest extends Frame {
- public IFrameTest(String comment) {
- super(comment);
- }
- public TextField input = new TextField( "輸入框" );// 定位里面的輸入框
- {
- input.addIDLocator("input1" );
- }
- }
- }
新建一個Test類,例子類目為P_Test,代碼如下:
- import org.junit.Test;
-
- import com.holmos.webtest.EngineType;
- import com.holmos.webtest.utils.HolmosWindow;
-
- public class P_Test {
- public P p = new P();
-
- @Test
- public void input() {
- HolmosWindow. openNewWindow(EngineType.WebDriverChrome, "D:\\main.html" );
- p.frame .select();// 選取要操作的frame
- p.frame .input.setText( "試試看!能不能輸入" );// 操作frame里面的元素
- p.frame .selectTopPage();// 控制器交還給最底層頁面
- }
- }
執(zhí)行結(jié)果:
控制臺輸出:.png)
多層iFrame定位思想是一個樣的,一層一層往里邊定位。