我想为一个问题创建多个答案,在excel中我有3行,答案1,答案2和问题1的答案3.我想根据问题名称重复3次答案循环。由于我使用Apache POI从excel中检索值,因此我的代码适用于第1行,但当它移动到第2行时,它也会尝试输入问题。
我想将第1行中的Question名称存储在临时变量中,当循环到达第2行时,它应该将临时变量中的值与第2行问题名称进行比较。如果问题名称相同,则不应重复输入问题,代码应仅创建答案。你能帮忙吗?以下是我使用的代码
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
答案 0 :(得分:0)
我不确定在阅读每个excel的行后你究竟想要实现什么。根据您在问题上的描述,这是一个可能的解决方案。请注意关于你的字符串比较的评论,这可能是错误本身,不知道你的代码实际上做了什么。
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
答案 1 :(得分:0)
简单的方法是创建一个ArrayList
并存储已在应用程序中输入的所有问题。因此,每当您要输入新问题时,只需检查输入的问题是否已存在于ArrayList中,如果已经可用则省略。看看下面的例子。
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);
}