【问题标题】:Need help for Java Condition Code Selenium需要 Java 条件代码 Selenium 的帮助
【发布时间】:2019-04-15 06:10:51
【问题描述】:

需要从下拉列表中选择。如果选择是,我需要填写某些文本字段,如果没有,则需要填写其他文本字段。使用 selenium java,我需要有关如何编写 if else 语句的帮助。 我已输入完整代码供您参考。

在此处输入代码

    <WebElement JointApplication=driver.findElement(By.id("-1-1-joint_life"));      
    Select Joint=new Select(JointApplication);

    Joint.selectByVisibleText("No");

    Thread.sleep(6000);

    if(driver.findElement(By.xpath("//SELECT[contains(@style,'width: 102%;')]")).isDisplayed())
    {
        // if we select yes for Joint Application

        WebElement Title=driver.findElement(By.id("dd-2-1-title1"));
        Select Tit=new Select(Title);
        Tit.selectByVisibleText("Mr");
        driver.findElement(By.id("-2-1-fullname1")).sendKeys("Test ");
        driver.findElement(By.id("-2-1-csurname1")).sendKeys("Automatioan");
        driver.findElement(By.id("-2-1-nino")).sendKeys("SK119944B");
        driver.findElement(By.id("-1-d_o_b1")).sendKeys("01");
        driver.findElement(By.id("-1-d_o_b2")).sendKeys("01");
        driver.findElement(By.id("-1-d_o_b3")).sendKeys("1990");




    }
    else 
    {

        WebElement COB=driver.findElement(By.id("-2-1-cntry_bth"));
        Select birth=new Select(COB);
        birth.selectByVisibleText("United Kingdom");
        driver.findElement(By.id("-2-1-place_bth")).sendKeys("London");
        Thread.sleep(2000);
        WebElement Nationality=driver.findElement(By.id("-2-1-nat_ality"));
        Select Nation=new Select(Nationality);
        Nation.selectByVisibleText("United Kingdom");



    }>

【问题讨论】:

  • 帮忙写什么?
  • 我正在使用 if else 语句,它没有执行 if else 语句。我只能执行一条语句
  • 你确定吗? ifelse 块中的代码完全相同。
  • 我的测试要求是,当应用程序联合时,它有不同的文本字段,如果是单个应用程序,则有差异字段。代码是正确的,但我正在努力使用 java 逻辑
  • @Arjun 您的代码说:“如果这个 contition 是真的,那么做这个,如果它不是真的,做同样的事情”。它应该这样做吗?

标签: java selenium selenium-webdriver automation automated-tests


【解决方案1】:

对于 IF 语句本身,做

if(JointApplication.getAttribute("value").contains("No")){
  //do this
} else {
  //do this
}

应该可以。它将查找 select 语句中设置的值。

虽然,我并不完全理解在这种情况下需要 if 语句。如果您知道将其设置为“是”或“否”,为什么不相应地填写其他字段?

【讨论】:

  • 嗨,我想使用 if else 语句的原因是如果我选择是,那么我需要填写 2 个应用程序,如果我选择否,我需要填写 1 个应用程序。我正在使用数据驱动框架和我无法执行是和否的完整代码
  • 嗨,Arild,你能帮忙解决以下问题吗(链接)sqa.stackexchange.com/q/36421/35535
猜你喜欢
  • 2011-02-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-24
  • 2018-01-27
  • 1970-01-01
  • 2021-07-23
  • 1970-01-01
相关资源
最近更新 更多