问题: 我有一个局部变量,需要在父类中用于其他方法。我使用了实例变量,但是发送键却出现错误
我尝试过/实验过的事情: 我已经声明了一个实例变量名String“ String AppID;”
我从实例变量实验中遇到的错误: org.openqa.selenium.WebDriverException:未知错误:密钥应为字符串
这是完整的代码:
public class NewWarrant extends TestBase {
Solicitation solicitation;
WorkloadManager workloadmanager;
String AppID; // Instance Variable Declared
public NewWarrant() {
super();
}
public void createNewWarrantPage() throws Exception {
/
WebDriverWait wait = new WebDriverWait(driver, 40);
// Navigating to the Warrant Page
driver.findElement(Transaction_Link).click();
driver.findElement(Acquisitions_Link).click();
driver.findElement(Additional_Form_Link).click();
driver.findElement(New_Link).click();
driver.findElement(Warrent_link).click();
// switching to page Iframe
WebElement iframe = driver.findElement(By.xpath("//*[@id='PegaGadgetIfr']"));
driver.switchTo().frame(iframe); // Filling out all data's for the page
driver.findElement(Warrent_Template_Field).sendKeys("CLASS_I");
//Sending the newly created KO User to Canidate ID field
synchoWait();
driver.findElement(By.id("CandidateOpID")).sendKeys(Keys.chord(Keys.CONTROL, "v"));
synchoWait();
driver.findElement(By.xpath("//*[@id='po0']")).click();
synchoWait();
driver.findElement(DoDDAC_Input_Field).sendKeys("W91QV1");
driver.findElement(PCO_CheckBox).click(); synchoWait();
driver.findElement(Limited_Radio_Button).click();
driver.findElement(Prejudice_radio_Button).click();
synchoWait();
driver.findElement(Semester_radio_Button).click();
synchoWait();
driver.findElement(Supervisor_Field).sendKeys("dschrute");
synchoWait();
driver.findElement(New_Warrant_Submit_Button).click();
synchoWait();
//Using the AppID instance variable to store the value using enterTextInField method.
AppID = getTextOfElement();
driver.switchTo().defaultContent();
driver.findElement(Profile_Dropdown).click();
driver.findElement(Sign_Out).click();
}
public void reviewAppByCanidate() throws Exception {
try {
synchronized (Task_Input_Field) {
Task_Input_Field.wait(2000);
driver.findElement(Task_Input_Field).sendKeys("Candidate Warrant Review");
}
} catch (Exception e) {
driver.findElement(Task_Input_Field).sendKeys("Candidate Warrant Review");
}
synchoWait();
// I am using App ID instance variable with enterTextInField method to send the values in to the field.
enterTextInField(AppID);
driver.findElement(Search_Button).click();
driver.findElement(Sort_Task_Button).click();
driver.findElement(Sort_By_Field).sendKeys("Assignment Date");
synchoWait();
driver.findElement(Sort_Decending_Arrow).click();
synchoWait();
driver.findElement(Sort_Button).click();
synchoWait();
driver.findElement(Box_Card).click();
synchoWait();
driver.findElement(Open_Button).click();
synchoWait();
WebElement iframe = driver.findElement(By.xpath("//*[@id='PegaGadgetIfr']"));
driver.switchTo().frame(iframe); // Filling out all data's for the page
driver.findElement(Cand_Review_Submit_Button).click();
driver.switchTo().defaultContent();
driver.findElement(Profile_Dropdown).click();
driver.findElement(Sign_Out).click();
}
// Get Text of Element to store in variable call "text"
public String getTextOfElement() {
//WebDriverWait wait = new WebDriverWait(driver, 30);
//wait.until(ExpectedConditions.visibilityOfElementLocated(Application_ID));
String text = driver.findElement(Application_ID).getText();
driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
System.out.println("My copied value: " + text);
// return elementText;
return text;
}
// Enter Text of element from stored variable from getTextOfElement()
public void enterTextInField(String value) {
driver.findElement(Item_Number_Field).clear();
driver.findElement(Item_Number_Field).sendKeys(value);
System.out.println("value copied");
}
}
答案 0 :(得分:2)
您不能在Cucumber步骤之间传递变量,因为每次执行新步骤时都会清除其值。
无论如何,请考虑重构测试以使用Page Object Model design pattern在测试逻辑和DOM元素定义之间引入抽象层。
答案 1 :(得分:0)
尝试使用操作执行相同的操作。它可能会起作用,并且如果您要执行粘贴操作,则尝试捕获变量中的文本,然后将其传递给键以发送其更好的解决方案,而不是复制粘贴。