如何在selenium webdriver中使用on / else条件和onclick?

时间:2016-02-17 08:31:10

标签: java selenium automation

在selenium webdriver中我想在java中使用if / else条件。需要单击每个步骤,并且需要执行一次。具有Number_Select.NumberRandom(driver,2).click(); =从numberRandom中选择if else语句。例如,所以我的问题是,一旦测试用例运行它只点击一个数字,假设点击2个随机数。请建议

TestCases:

@Test(description = "Login Page Test" , enabled = true)
    public void faaaa() throws Exception {
        try{
            driver = new FirefoxDriver();
            SignIn_Action.Execute(driver);
            Menu_Select.Menu_Selection(driver, 2).click();
            MenuLottery_Select.Menu_Selection(driver, 1).click();
            MenuSubTab_Select.MenuSubTab_1(driver, 3).click();
            Number_Select.NumberRandom(driver, 2).click();
            //SignIn_Action.Logout_Session(driver);
            //Log.info("Login Successful");
        }catch (Exception e){

            System.out.println("Test 1"); 
            //Log.error(e.getMessage());
            throw (e);
        }

    }

Number_Select:

public static WebElement NumberRandom(WebDriver driver, int selection)
            throws Exception {

        if (selection == 1) {
            Thread.sleep(1500);
            element = driver.findElement(By
                    .xpath("//*[@id='lottery']/div[3]/dl/dd/i[8]"));
        } else if (selection == 2) {
            Thread.sleep(1500);
            element = driver.findElement(By
                    .xpath("//*[@id='lottery']/div[3]/dl[1]/dd/i[8]"));
            element = driver.findElement(By
                    .xpath("//*[@id='lottery']/div[3]/dl[2]/dd/i[3]"));
        } else if (selection == 3) {
            Thread.sleep(1500);
            element = driver.findElement(By
                    .xpath("//*[@id='lottery']/div[3]/dl[1]/dd/i[8]"));
            element = driver.findElement(By
                    .xpath("//*[@id='lottery']/div[3]/dl[2]/dd/i[3]"));
            element = driver.findElement(By
                    .xpath("//*[@id='lottery']/div[3]/dl[3]/dd/i[5]"));
        } else if (selection == 4) {
            Thread.sleep(1500);
            element = driver.findElement(By
                    .xpath("//*[@id='lottery']/div[3]/dl[1]/dd/i[8]"));
            element = driver.findElement(By
                    .xpath("//*[@id='lottery']/div[3]/dl[2]/dd/i[3]"));
            element = driver.findElement(By
                    .xpath("//*[@id='lottery']/div[3]/dl[3]/dd/i[5]"));
            element = driver.findElement(By
                    .xpath("//*[@id='lottery']/div[3]/dl[4]/dd/i[1]"));
        } else if (selection == 5) {
            Thread.sleep(1500);
            element = driver.findElement(By.xpath("//*[@id='lottery']/div[3]/dl[1]/dd/i[8]"));
            element = driver.findElement(By
                    .xpath("//*[@id='lottery']/div[3]/dl[2]/dd/i[3]"));
            element = driver.findElement(By
                    .xpath("//*[@id='lottery']/div[3]/dl[3]/dd/i[5]"));
            element = driver.findElement(By
                    .xpath("//*[@id='lottery']/div[3]/dl[4]/dd/i[1]"));
            element = driver.findElement(By
                    .xpath("//*[@id='lottery']/div[3]/dl[5]/dd/i[9]"));//*[@id="lottery"]/div[3]/dl[1]/dd/i[1]
        } else {
            Log.error("Unable to select product list");
            System.out.println("Unable select product list");
        }

        return element;
    }

1 个答案:

答案 0 :(得分:2)

NumberRandom只返回一个WebElement。即使您每次覆盖最后一个值if而在同一element块中找到多个Web元素,最后一个值也会返回到faaaa()。如果要单击所需的所有元素,请将click()插入方法

else if (selection == 2) {
    Thread.sleep(1500);
     element = driver.findElement(By.xpath("//*[@id='lottery']/div[3]/dl[1]/dd/i[8]")).click();
     element = driver.findElement(By.xpath("//*[@id='lottery']/div[3]/dl[2]/dd/i[3]")).click();
}

作为旁注,您应该避免使用Thread.sleep。如果您想确保元素是可点击的,请使用显式等待和expected conditions

WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//*[@id='lottery']/div[3]/dl[1]/dd/i[8]"))).click();
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//*[@id='lottery']/div[3]/dl[2]/dd/i[3]"))).click();

在点击之前,元素可以等待最多10秒钟。