@FindBy 错误 class Select
@FindBy error With class Select
我正在尝试在 class Select
中实现带有注释 @FindBy
的页面对象。在 Eclipse 中,它显示以下消息:
the method id(String) in the type By is not applicable for the arguments (WebElement).
我不明白为什么会出现此消息。按照下面的错误代码和图像。
Class FaturamentoGeTratamentoOsPage
public class FaturamentoGeTratamentoOsPage {
WebDriver driver;
@FindBy(id = "cboMotivo")
WebElement CBOMotivo;
public FaturamentoGeTratamentoOsPage(WebDriver driver) {
this.driver = driver;
}
public void preencherCampoMotivo(String CampoMotivo) {
// Campo Motivo
WebElement campoMotivo = driver.findElement(By.id(CBOMotivo));
Select slcMotivo = new Select(campoMotivo);
slcMotivo.selectByVisibleText(CampoMotivo);
}
public void preencherCampoSubmotivo(String CampoSubMotivo) throws Exception {
}
}
Class FaturamentoGeConectividadeFacilidadesTest
public class FaturamentoGeConectividadeFacilidadesTest {
static WebDriver driver;
@Before
public void setUp() throws Exception {
SelecionarNavegador nav = new SelecionarNavegador();
driver = nav.iniciarNavegador("chrome", "http://10.5.9.45/BkoMais_Selenium/");
}
@Test
public void selecionarFacilidades() throws Exception {
// Logando na aplicação
LogarBkoMaisPage login = new LogarBkoMaisPage(driver);
login.logar("844502", "Bcc201707");
// BackOffice >> FaturamentoGe >> Conectividade
FaturamentoGeConectividadeFacilidadesPage menu = new FaturamentoGeConectividadeFacilidadesPage(driver);
menu.logarFaturamentoGeConectividade();
//Registro >> Novo caso
RegistroNovoCasoPage reg = new RegistroNovoCasoPage(driver);
reg.registrarCaso();
//Preencher campos
FaturamentoGeTratamentoOsPage campo = new FaturamentoGeTratamentoOsPage(driver);
campo.preencherCampoMotivo(" Concluido ");
}
@After
public void tearDown() throws Exception {
Thread.sleep(5000);
driver.quit();
}
}
您需要添加pagefactory.init
来初始化webelement。
public FaturamentoGeTratamentoOsPage(WebDriver driver) { this.driver = driver;
PageFactory.initElements(driver, this);
}
不使用下面的行.. 因为 CBOMotive 直接 returns 你只是一个 webelement
WebElement campoMotivo = driver.findElement(By.id(CBOMotivo));
您收到错误是因为 By.id()
需要 String
,而不是 WebElement
。您已将 CBOMotivo
定义为 WebElement
,但将其视为 String
。
以下是正确的用法
WebElement campoMotivo = driver.findElement(By.id("cboMotivo"));
你要的是
public void preencherCampoMotivo(String CampoMotivo) {
// Campo Motivo
Select slcMotivo = new Select(CBOMotivo);
slcMotivo.selectByVisibleText(CampoMotivo);
}
我正在尝试在 class Select
中实现带有注释 @FindBy
的页面对象。在 Eclipse 中,它显示以下消息:
the method id(String) in the type By is not applicable for the arguments (WebElement).
我不明白为什么会出现此消息。按照下面的错误代码和图像。
Class FaturamentoGeTratamentoOsPage
public class FaturamentoGeTratamentoOsPage {
WebDriver driver;
@FindBy(id = "cboMotivo")
WebElement CBOMotivo;
public FaturamentoGeTratamentoOsPage(WebDriver driver) {
this.driver = driver;
}
public void preencherCampoMotivo(String CampoMotivo) {
// Campo Motivo
WebElement campoMotivo = driver.findElement(By.id(CBOMotivo));
Select slcMotivo = new Select(campoMotivo);
slcMotivo.selectByVisibleText(CampoMotivo);
}
public void preencherCampoSubmotivo(String CampoSubMotivo) throws Exception {
}
}
Class FaturamentoGeConectividadeFacilidadesTest
public class FaturamentoGeConectividadeFacilidadesTest {
static WebDriver driver;
@Before
public void setUp() throws Exception {
SelecionarNavegador nav = new SelecionarNavegador();
driver = nav.iniciarNavegador("chrome", "http://10.5.9.45/BkoMais_Selenium/");
}
@Test
public void selecionarFacilidades() throws Exception {
// Logando na aplicação
LogarBkoMaisPage login = new LogarBkoMaisPage(driver);
login.logar("844502", "Bcc201707");
// BackOffice >> FaturamentoGe >> Conectividade
FaturamentoGeConectividadeFacilidadesPage menu = new FaturamentoGeConectividadeFacilidadesPage(driver);
menu.logarFaturamentoGeConectividade();
//Registro >> Novo caso
RegistroNovoCasoPage reg = new RegistroNovoCasoPage(driver);
reg.registrarCaso();
//Preencher campos
FaturamentoGeTratamentoOsPage campo = new FaturamentoGeTratamentoOsPage(driver);
campo.preencherCampoMotivo(" Concluido ");
}
@After
public void tearDown() throws Exception {
Thread.sleep(5000);
driver.quit();
}
}
您需要添加pagefactory.init
来初始化webelement。
public FaturamentoGeTratamentoOsPage(WebDriver driver) { this.driver = driver;
PageFactory.initElements(driver, this);
}
不使用下面的行.. 因为 CBOMotive 直接 returns 你只是一个 webelement
WebElement campoMotivo = driver.findElement(By.id(CBOMotivo));
您收到错误是因为 By.id()
需要 String
,而不是 WebElement
。您已将 CBOMotivo
定义为 WebElement
,但将其视为 String
。
以下是正确的用法
WebElement campoMotivo = driver.findElement(By.id("cboMotivo"));
你要的是
public void preencherCampoMotivo(String CampoMotivo) {
// Campo Motivo
Select slcMotivo = new Select(CBOMotivo);
slcMotivo.selectByVisibleText(CampoMotivo);
}