[英]Selenium Webdriver - Java - How to store a value read from excel in temporary variable
I would like to create multiple answers for a question, In excel I have 3 rows with Answer 1 , Answer 2 and Answer 3 for Question 1. I would like to repeat the answer loop 3 times based on the Question name. 我想为一个问题创建多个答案,在excel中,我有3行,其中包含问题1的答案1,答案2和答案3。我想根据问题名称重复3次答案循环。 Since I'm using Apache POI to retrieve the value from excel my code works good for the 1st row but when it moves to the 2nd row it tries to input the question also. 由于我使用Apache POI从excel检索值,因此我的代码对于第一行效果很好,但是当它移至第二行时,它也会尝试输入问题。
I would like to store the Question name from row 1 in temporary varaible and when loop gets to 2nd row it should compare the value in temporary variable with 2nd row question name. 我想将第1行的问题名称存储在临时变量中,当循环到达第二行时,应将临时变量中的值与第二行问题名称进行比较。 If the question name is same then it should NOT repeat entering the question, Code should create only answer. 如果问题名称相同,则不应重复输入问题,代码应仅创建答案。 Can you please help on this. 您能帮上忙吗? Below is the code that I used 以下是我使用的代码
driver.get(new URI(driver.getCurrentUrl()).resolve("/questions/question/create").toString());
WaituntilElementpresent.isExist();
for (int j=1; j <= sheet1.getLastRowNum(); j++)
{
String question_name = sheet1.getRow(j).getCell(0).getStringCellValue();
String question_text = sheet1.getRow(j).getCell(1).getStringCellValue();
String question_type = sheet1.getRow(j).getCell(2).getStringCellValue();
String question_naire = sheet1.getRow(j).getCell(3).getStringCellValue();
String answer_name = sheet1.getRow(j).getCell(4).getStringCellValue();
String answer_value_fillup = sheet1.getRow(j).getCell(5).getStringCellValue();
int answer_value_int = (int) sheet1.getRow(j).getCell(6).getNumericCellValue();
double answer_value_deci = sheet1.getRow(j).getCell(7).getNumericCellValue();
String answer_weight = sheet1.getRow(j).getCell(8).getStringCellValue();
String answer_comparison = sheet1.getRow(j).getCell(9).getStringCellValue();
String response_text = sheet1.getRow(j).getCell(10).getStringCellValue();
String option_text = sheet1.getRow(j).getCell(11).getStringCellValue();
String option_correct = sheet1.getRow(j).getCell(12).getStringCellValue();
WaituntilElementpresent.isExist();
//Boolean ques_name = driver.getPageSource().matches(question_name);
String ques_name= driver.findElement(By.id("name")).getText();
//String ques_name1= sheet1.getTopRow().getCell(0).getStringCellValue();;
WaituntilElementpresent.isExist();
System.out.println("Question name from Webpage -->"+ques_name);
System.out.println("Excel Question name -->"+question_name);
Thread.sleep(5000);
if (ques_name != question_name)
{
//Call the method
add_question_page.Add_Question_MI(driver,question_name,question_text,question_type,question_naire);
WaituntilElementpresent.isExist();
} //for if
String parentWindowHandler = driver.getWindowHandle();
add_question_page.Add_Answer_MI(driver);
WaituntilElementpresent.isExist();
CallAnswer(driver);
WaituntilElementpresent.isExist();
CallAnswerbasedonQuestiontype(driver,question_type, answer_name,answer_value_fillup,answer_value_int,answer_value_deci,answer_weight,answer_comparison,response_text,option_text, option_correct);
WaituntilElementpresent.isExist();
add_question_page.Save_Answer_MI(driver,parentWindowHandler);
} //for j
I'm not sure what exactly it is you're trying to achieve after reading each excel's row. 我不确定在阅读每个excel行之后您到底想达到什么目的。 Based on what you're describing on your question, this is a possible solution. 根据您对问题的描述,这是一个可能的解决方案。 Please note the comments about your String comparison, which may be the error in itself, not knowing what your code actually does in that if. 请注意有关String比较的注释,这本身可能就是错误,而不知道代码在其中的实际作用。
driver.get(new URI(driver.getCurrentUrl()).resolve("/questions/question/create").toString());
WaituntilElementpresent.isExist();
String prevQuestionName=null;
for (int j=1; j <= sheet1.getLastRowNum(); j++)
{
String question_name = sheet1.getRow(j).getCell(0).getStringCellValue();
String question_text = sheet1.getRow(j).getCell(1).getStringCellValue();
String question_type = sheet1.getRow(j).getCell(2).getStringCellValue();
String question_naire = sheet1.getRow(j).getCell(3).getStringCellValue();
String answer_name = sheet1.getRow(j).getCell(4).getStringCellValue();
String answer_value_fillup = sheet1.getRow(j).getCell(5).getStringCellValue();
int answer_value_int = (int) sheet1.getRow(j).getCell(6).getNumericCellValue();
double answer_value_deci = sheet1.getRow(j).getCell(7).getNumericCellValue();
String answer_weight = sheet1.getRow(j).getCell(8).getStringCellValue();
String answer_comparison = sheet1.getRow(j).getCell(9).getStringCellValue();
String response_text = sheet1.getRow(j).getCell(10).getStringCellValue();
String option_text = sheet1.getRow(j).getCell(11).getStringCellValue();
String option_correct = sheet1.getRow(j).getCell(12).getStringCellValue();
WaituntilElementpresent.isExist();
//Boolean ques_name = driver.getPageSource().matches(question_name);
String ques_name= driver.findElement(By.id("name")).getText();
//String ques_name1= sheet1.getTopRow().getCell(0).getStringCellValue();;
WaituntilElementpresent.isExist();
System.out.println("Question name from Webpage -->"+ques_name);
System.out.println("Excel Question name -->"+question_name);
Thread.sleep(5000);
if(prevQuestionName == null || !prevQuestionName.equals(question_name)) {
//fill in question
}
//use String's .equals() method instead of != here.
if (ques_name != question_name)
{
//Call the method
add_question_page.Add_Question_MI(driver,question_name,question_text,question_type,question_naire);
WaituntilElementpresent.isExist();
} //for if
String parentWindowHandler = driver.getWindowHandle();
add_question_page.Add_Answer_MI(driver);
WaituntilElementpresent.isExist();
CallAnswer(driver);
WaituntilElementpresent.isExist();
CallAnswerbasedonQuestiontype(driver,question_type, answer_name,answer_value_fillup,answer_value_int,answer_value_deci,answer_weight,answer_comparison,response_text,option_text, option_correct);
WaituntilElementpresent.isExist();
add_question_page.Save_Answer_MI(driver,parentWindowHandler);
} //for j
Easy way of doing is by just create an ArrayList
and store all questions that has been already entered in the application. 简单的方法是仅创建一个ArrayList
并存储已在应用程序中输入的所有问题。 So each time when you are going to enter a new question just check whether the entered question is already there in the ArrayList,if already available then omit. 因此,每次您要输入新问题时,只需检查输入的问题是否已经存在于ArrayList中,如果已经可用,则忽略。 have a look at below example. 看下面的例子。
driver.get(new URI(driver.getCurrentUrl()).resolve("/questions/question/create").toString());
WaituntilElementpresent.isExist();
List<String> enteredQuestions = new ArrayList<String>();
for (int j=1; j <= sheet1.getLastRowNum(); j++)
{
/*All your logics go here*/
if (!enteredQuestions.contains(question_name))
{
add_question_page.Add_Question_MI(driver,question_name,question_text,question_type,question_naire);
enteredQuestions.add(question_name);
WaituntilElementpresent.isExist();
} //for if
String parentWindowHandler = driver.getWindowHandle();
add_question_page.Add_Answer_MI(driver);
WaituntilElementpresent.isExist();
CallAnswer(driver);
WaituntilElementpresent.isExist();
CallAnswerbasedonQuestiontype(driver,question_type, answer_name,answer_value_fillup,answer_value_int,answer_value_deci,answer_weight,answer_comparison,response_text,option_text, option_correct);
WaituntilElementpresent.isExist();
add_question_page.Save_Answer_MI(driver,parentWindowHandler);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.