2016-11-09 47 views
0

我想爲問題創建多個答案,在Excel中,對於問題1,我有3行,包含答案1,答案2和答案3。希望根據問題名稱重複回答3次。由於我使用Apache POI從Excel中檢索值,因此我的代碼適用於第一行,但當它移動到第二行時,它也會嘗試輸入問題。Selenium Webdriver - Java - 如何將從excel中讀取的值存儲在臨時變量中

我想在臨時變量中存儲第1行的問題名稱,當循環到達第2行時,它應該將臨時變量中的值與第2行問題名稱進行比較。如果問題的名稱相同,則不應重複輸入問題,代碼應僅創建答案。你能幫忙嗎?下面是我使用的代碼

 driver.get(new URI(driver.getCurrentUrl()).resolve("https://stackoverflow.com/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

簡單的做法是創建一個ArrayList並存儲已經輸入到應用程序中的所有問題。所以每當你打算輸入一個新的問題時,只要檢查輸入的問題是否已經存在於ArrayList中,如果已經存在,那麼省略。看看下面的例子。

driver.get(new URI(driver.getCurrentUrl()).resolve("https://stackoverflow.com/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); 

     } 
+0

謝謝Sudharsan !!它的工作 – Selvi

+0

很高興它的工作:) –

0

我不確定在讀完每個excel的行後你到底想達到什麼目的。根據你所描述的問題,這是一個可能的解決方案。請注意關於你的字符串比較的評論,這可能是錯誤本身,不知道你的代碼實際上做了什麼,如果。

driver.get(new URI(driver.getCurrentUrl()).resolve("https://stackoverflow.com/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 
相關問題