我有以下代码,当它们通过第一个if条件时,我们给出了'!'。否则它会通过else部分。

时间:2015-08-04 05:35:52

标签: selenium-webdriver

if(!"".equals(MyFrnds.list_Friends))
        {
            System.out.println("There is friends list");

            Actions action= new Actions(driver);
            action.contextClick(MyFrnds.list_Friends).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.RETURN).build().perform();
            System.out.println("My first friend link is opened in new tab and clicking on Show more button if there are more than 12 friends ");
            if(!"".equals(MyFrnds.list_Friends))
            {
             MyFrnds.btn_FrShowmore.click();
            }else
            {
                System.out.println("There are no more than 12 records");
            }
        }

        else
        {
            System.out.println("There are no friends to display.  So clicking on these two links to add friends");

            // Right on FITBASE MEMBERS link and open in new tab
        Actions action= new Actions(driver);
        action.contextClick(MyFrnds.lnk_Fitbasemem).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.RETURN).build().perform();
            // Right on Social network link and open in new tab
        action.contextClick(MyFrnds.lnk_Socialnet).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.RETURN).build().perform();
        }

在第一行的上述代码中,我将If条件赋予(!"" .equals(MyFrnds.list_Friends)),但无论应用程序如何,它都会进入条件的第一部分,即使它不满足第一个条件。因此我们在执行脚本时遇到错误。任何人都可以在代码中提出错误的建议。

1 个答案:

答案 0 :(得分:1)

如果!"".equals(MyFrnds.list_Friends)不是空字符串,则

trueMyFrnds.list_Friends。例如,如果MyFrnds.list_Friendsnull,则条件也会返回true。您可能需要执行额外的null检查,或者只使用StringUtils.isNotBlank(MyFrnds.list_Friends)

Java, check whether a string is not null and not empty?