등, 적은 오래된 요소 문제를 얻을 : https://stackoverflow.com/a/3987430/1073584 을 마지막으로 내가 3 개 클래스를 구현하여 원하는 것을 얻을 수 있었다 : CustomPageFactory, CustomElementLocator, CustomAnnotations.
지금은
// ======= LoginPage
public class LoginPage extends AbstractPage {
Config config;
@FindBy(using = "CONFIG") // take locator from config
WebElement passwordFld;
// .. other fields skipped
public LoginPage(WebDriver webDriver, Config config) throws IOException {
super(webDriver);
this.config = config;
PageFactory.initElements(new CustomPageFactory(webDriver, config), this);
}
}
// == 설정 을 형태 보증 된 페이지 개체를 초기화하기 위해서 다음과 같은 코드를 사용하여 내 로케이터를 구체화 할 수 있어요 === login.page.typesafe.설정 :
passwordFld = {
name = "password field"
}
// ============= CustomPageFactory
package com.company.pages.support;
import com.typesafe.config.Config;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.support.pagefactory.ElementLocator;
import org.openqa.selenium.support.pagefactory.ElementLocatorFactory;
import java.lang.reflect.Field;
public class CustomPageFactory implements ElementLocatorFactory {
private Config config;
private WebDriver driver;
public CustomPageFactory(WebDriver driver, Config config) {
this.driver = driver;
this.config = config;
}
public ElementLocator createLocator(Field field) {
return new CustomElementLocator(driver, field, config);
}
}
// ================ = CustomElementLocator
package com.company.pages.support;
import com.typesafe.config.Config;
import org.openqa.selenium.SearchContext;
import org.openqa.selenium.support.pagefactory.DefaultElementLocator;
import java.lang.reflect.Field;
public class CustomElementLocator extends DefaultElementLocator {
private Config config;
public CustomElementLocator(SearchContext searchContext, Field field, Config config) {
super(searchContext, new CustomAnnotations(field, config));
this.config = config;
}
}
// ====== CustomAnnotations
package com.company.pages.support;
import com.typesafe.config.Config;
import com.typesafe.config.ConfigObject;
import org.openqa.selenium.By;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.pagefactory.Annotations;
import java.lang.reflect.Field;
public class CustomAnnotations extends Annotations {
Config config;
public CustomAnnotations(Field field, Config config) {
super(field);
this.config = config;
}
@Override
protected By buildByFromShortFindBy(FindBy findBy) {
if (findBy.using().equals("CONFIG")) {
if (null != config) {
ConfigObject fieldLocators = config.getObject(getField().getName());
if (fieldLocators.keySet().contains("className"))
return By.className(fieldLocators.get("className").unwrapped().toString());
if (fieldLocators.keySet().contains("css"))
return By.cssSelector(fieldLocators.get("css").unwrapped().toString());
if (fieldLocators.keySet().contains("id"))
return By.id(fieldLocators.get("id").unwrapped().toString());
if (fieldLocators.keySet().contains("linkText"))
return By.linkText(fieldLocators.get("linkText").unwrapped().toString());
if (fieldLocators.keySet().contains("name"))
return By.name(fieldLocators.get("name").unwrapped().toString());
if (fieldLocators.keySet().contains("partialLinkText"))
return By.partialLinkText(fieldLocators.get("partialLinkText").unwrapped().toString());
if (fieldLocators.keySet().contains("tagName"))
return By.tagName(fieldLocators.get("tagName").unwrapped().toString());
if (fieldLocators.keySet().contains("xpath"))
return By.xpath(fieldLocators.get("xpath").unwrapped().toString());
}
}
return super.buildByFromShortFindBy(findBy);
}
}
당신의 로케이터 많이를 변경하는 페이지 클래스 내에서이 작업을 수행 할 수 있습니다? 왜 쉽게 찾을 수 있고 정리되어있는 페이지 개체에 파일을 넣는 대신 외부 파일에서 읽으려고합니까? – JeffC
1. ios 및 Android 장치에 대한 플랫폼 독립적 테스트를 작성하려고하며 페이지 개체를 복제하려고하지 않습니다. 2. 외부화 된 로케이터가 유용 할 다음 지점이 될 수 있습니다. 3. 일부 로케이터는 런타임 중에 변경할 수 있습니다 (다른 순서로 정렬 된 요소들) – ludenus