简体   繁体   中英

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. 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.

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. 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. 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.

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. 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. 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);

         }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM