我用if-else语句编写了一个Java代码,用户将通过4个备用路由中的任意一个从“Screen1”移动到“Screen2”,并为所有可能的应用程序流附加了一个图像当然是由开发人员编写的代码决定的。只是添加使用的工具是Appium。
driver.findElement(By.id("----")).click(); //this click will take from 'screen1' to next screen.
if(driver.findElement(By.id("com.abc.rbanking:id/WhatsNew")).isDisplayed())
{ //case if screen A is displayed just after screen 1
MobileElement cross = driver.findElement(By.xpath("//*[@class = 'android.widget.ImageView']"));
cross.click();
Thread.sleep(3000);
}
if(driver.findElement(By.xpath("//android.widget.TextView[@resource-id='com.abc.rbanking:id/text_logo'][text()='Security Question']")).isDisplayed())
{ //case when screen B is displayed Just after screen 1
MobileElement mfaQ = driver.findElement(By.id("com.abc.rbanking:id/MfaQuestionText"));
String question = mfaQ.getText();
String lastword = question.replaceAll("^.*?(\\w+)\\W*$", "$1");
System.out.println(lastword);
MobileElement answer = driver.findElement(By.id("com.abc.rbanking:id/MfaAnswerTextBox"));
answer.sendKeys(lastword);
MobileElement checkbox = driver.findElement(By.id("com.abc.rbanking:id/ShowChallengeAnswerCheckbox"));
checkbox.click();
Thread.sleep(3000);
MobileElement nextb = driver.findElement(By.id("com.abc.rbanking:id/PrimaryButton"));
nextb.click();
Thread.sleep(8000);
}
if(driver.findElement(By.id("com.abc.rbanking:id/WhatsNew")).isDisplayed())
{ //case when screen A is displayed after screen B
MobileElement cross = driver.findElement(By.xpath("//*[@class = 'android.widget.ImageView']"));
cross.click();
Thread.sleep(3000);
}
driver.findElement(By.id("----")); //this is code for 'Screen 2'
在执行脚本期间会发生什么,首先检查'If'并停止所有代码。我无法弄清楚这个问题。请帮忙。
答案 0 :(得分:1)
这听起来似乎(没有双关语意)你实际上不想要一个if else
构造。因此,请尝试使用单独的if
语句:
// see if element on 'screen A' is displayed
if (driver.findElement(By.id("abc")).isDisplayed()) {
//execute a few statements
}
// see if element on 'Screen B' is displayed
if (driver.findElement(By.id("xyz")).isDisplayed()) {
// execute a few statements
}
// see if element on 'screen A' is displayed
if (driver.findElement(By.id("abc")).isDisplayed()) {
}
driver.findElement(By.id("----")); //this is code for 'Screen 2'
您描述的行为,即第一个if
被击中而没有其他任何执行,正是您的代码应该如何表现。如果您打算允许每个代码块可能执行,那么我上面给出的是一个选项。
答案 1 :(得分:0)
最后,我能够找到问题的解决方案,并且工作正常。我把if语句放在try-catch块中,就像下面的代码一样,它对于应用程序给最终用户提出的每个替代方案都很好。
try {
if (driver.findElement(By.xpath("//android.widget.TextView[@resource-id='com.abc.rbanking:id/text_logo'][text()='Security Question']")).isDisplayed()) { //case when screen B is displayed Just after screen 1
MobileElement mfaQ = driver.findElement(By.id("com.abc.rbanking:id/MfaQuestionText"));
String question = mfaQ.getText();
String lastword = question.replaceAll("^.*?(\\w+)\\W*$", "$1");
System.out.println(lastword);
MobileElement answer = driver.findElement(By.id("com.abc.rbanking:id/MfaAnswerTextBox"));
answer.sendKeys(lastword);
MobileElement checkbox = driver.findElement(By.id("com.abc.rbanking:id/ShowChallengeAnswerCheckbox"));
checkbox.click();
Thread.sleep(3000);
MobileElement nextb = driver.findElement(By.id("com.abc.rbanking:id/PrimaryButton"));
nextb.click();
Thread.sleep(8000);
}
} catch (
Exception e) {
e.printStackTrace();
}
try
{
if (driver.findElement(By.id("com.abc.rbanking:id/WhatsNew")).isDisplayed()) { //case when screen A is displayed after screen B
MobileElement cross = driver.findElement(By.xpath("//*[@class = 'android.widget.ImageView']"));
cross.click();
Thread.sleep(3000);
}
} catch (
Exception e)
{
e.printStackTrace();
}
}