selenium中AjaxElementLocatorFactory的用法
阿新 • • 發佈:2018-12-28
先檢視一下AjaxElementLocatorFactory的源生程式碼
package org.openqa.selenium.support.pagefactory; import org.openqa.selenium.SearchContext; import java.lang.reflect.Field; public class AjaxElementLocatorFactory implements ElementLocatorFactory { private final SearchContext searchContext; private final int timeOutInSeconds; public AjaxElementLocatorFactory(SearchContext searchContext, int timeOutInSeconds) { this.searchContext = searchContext; this.timeOutInSeconds = timeOutInSeconds; } public ElementLocator createLocator(Field field) { return new AjaxElementLocator(searchContext, field, timeOutInSeconds); } }
AjaxElementLocatorFactory類是為了網頁動態程式碼而存在的。最終目的是拿到一個ElementLocator類的物件。
首先需要定義一個AjaxElementLocatorFactory的類
Class<?> cls = Class.forName("com.testReflect.FieldDemo"); //使用FieldDemo類的class物件生成 例項 Object obj = cls.newInstance(); //通過Class類中getField(String name): 獲取類特定的方法,name引數指定了屬性的名稱 Field field = cls.getField("num1"); AjaxElementLocatorFactory ajaxElementLocatorFactory = new AjaxElementLocatorFactory(driver, 5000); ElementLocator elementLocator = ajaxElementLocatorFactory.createLocator(field);