java—如何一次处理两个条件

x4shl7ld  于 2021-06-29  发布在  Java
关注(0)|答案(0)|浏览(179)

我的设想是:我正在自动化一个移动应用程序,它是一个教育平台。在我需要点击选项,如单一答案类型,数字类型,整数类型等。。
这里首先我需要选择单一答案类型选项,然后我需要移动到数字类型的问题(这必须重复所有科目,如数学,物理,化学)
那么有谁能建议如何处理这2种情况(适用于所有受试者)
下面是我的代码,它只适用于1个科目(我们希望它应该重复所有3个科目)

driver.manage().timeouts().implicitlyWait(100, TimeUnit.SECONDS);
    List<AndroidElement> singleanswer = (List<AndroidElement>) driver.findElementsByAndroidUIAutomator("new UiSelector().text(\"Single answer type\").index(2)");

    for(int i=0; i<=19; i++)
    {           
        driver.manage().timeouts().implicitlyWait(100, TimeUnit.SECONDS);
         MobileElement option1 = (MobileElement) driver.findElementByAndroidUIAutomator("new UiSelector().text(\"A\").index(0)");
         if(option1.isDisplayed())
         {
             option1.click();
             System.out.println("Single - Option1 click successfully");
         }
         else
         {
             System.out.println("Single - Option1 not clickable");
             File file  = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
             File f=new File("C:\\Users\\ssc\\Documents\\Srinu\\Appium\\images\\option1.PNG");
             FileUtils.copyFile(file, f);
         }

         driver.manage().timeouts().implicitlyWait(100, TimeUnit.SECONDS);
         MobileElement option2 = (MobileElement) driver.findElementByAndroidUIAutomator("new UiSelector().text(\"B\").index(0)");
         if(option2.isDisplayed())
         {
             option2.click();
             System.out.println("Single - Option2 click successfully");
         }
         else
         {
             System.out.println("Single - Option2 not clickable");
             File file  = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
             File f=new File("C:\\Users\\ssc\\Documents\\Srinu\\Appium\\images\\option2.PNG");
             FileUtils.copyFile(file, f);
         }

         driver.manage().timeouts().implicitlyWait(100, TimeUnit.SECONDS);
         MobileElement option3 = (MobileElement) driver.findElementByAndroidUIAutomator("new UiSelector().text(\"C\").index(0)");
         if(option3.isDisplayed())
         {
             option3.click();
             System.out.println("Single - Option3 click successfully");
         }
         else
         {
             System.out.println("Single - Option3 not clickable");
             File file  = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
             File f=new File("C:\\Users\\ssc\\Documents\\Srinu\\Appium\\images\\option3.PNG");
             FileUtils.copyFile(file, f);
         }

         driver.manage().timeouts().implicitlyWait(100, TimeUnit.SECONDS);
         MobileElement option4 = (MobileElement) driver.findElementByAndroidUIAutomator("new UiSelector().text(\"D\").index(0)");
         if(option4.isDisplayed())
         {
             option4.click();
             System.out.println("Single - Option4 click successfully" + "\n");
         }
         else
         {
             System.out.println("Single - Option4 not clickable");
             File file  = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
             File f=new File("C:\\Users\\ssc\\Documents\\Srinu\\Appium\\images\\option4.PNG");
             FileUtils.copyFile(file, f);
         }

         String up = "new UiSelector().text(\"Single answer type\")";
         String upcommand = "new UiScrollable(new UiSelector().scrollable(true).instance(0)).scrollIntoView(" + up + ");";
         driver.findElementByAndroidUIAutomator(upcommand); 

         MobileElement saveandexit = (MobileElement) driver.findElementByAndroidUIAutomator("new UiSelector().text(\"SAVE & NEXT\").index(0)");
         saveandexit.click();

         driver.manage().timeouts().implicitlyWait(200, TimeUnit.SECONDS);
         MobileElement previousbuttonDown = (MobileElement) driver.findElementByAndroidUIAutomator("new UiSelector().text(\"PREVIOUS\").index(0)");
         if (previousbuttonDown.isDisplayed()) 
         {
                try 
                {
                    System.out.println("Previousbutton : Displayed successfully - Test Pass" + "\n"); 
                } 
                catch (Exception e) 
                {
                    System.out.println("\"Previousbutton : Displaying from successfully - Test Fail\" + \"\\n\"");
                }
         }

         if(i==20)
         {
             break;
         }               

        }
    List<AndroidElement> numerictype=(List<AndroidElement>) driver.findElementsByAndroidUIAutomator("new UiSelector().text(\"Numeric type\").index(2)");
     for(int j=0; j<=4; j++)
     {
         MobileElement numericplusminus = (MobileElement) driver.findElementByAndroidUIAutomator("new UiSelector().text(\"+/-\").index(0)");
         numericplusminus.click();
         System.out.println("Numeric value" + numericplusminus);
         MobileElement numeric1 = (MobileElement) driver.findElementByAndroidUIAutomator("new UiSelector().text(\"1\").index(0)");
         numeric1.click();
         System.out.println("Numeric value" + numeric1.getText());
         MobileElement numeric2 = (MobileElement) driver.findElementByAndroidUIAutomator("new UiSelector().text(\"2\").index(0)");
         numeric2.click();
         System.out.println("Numeric value" + numeric2.getText());
         MobileElement numeric3 = (MobileElement) driver.findElementByAndroidUIAutomator("new UiSelector().text(\"3\").index(0)");
         numeric3.click();
         System.out.println("Numeric value" + numeric3.getText());
         MobileElement numeric4 = (MobileElement) driver.findElementByAndroidUIAutomator("new UiSelector().text(\"4\").index(0)");
         numeric4.click();
         System.out.println("Numeric value" + numeric4.getText());

         MobileElement saveandexit1 = (MobileElement) driver.findElementByAndroidUIAutomator("new UiSelector().text(\"SAVE & NEXT\").index(0)");
         saveandexit1.click();

         if(j==5)
         {
             break;
         }
     }

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题