I am busy testing automation. WebDriiver + Java. PageObject + PageFactory.

Here is the code for the page:

public ManageSubscriptionsTab hitResumeButtonIfDisplayed ( ){ if (resumeSubscriptionButton.isDisplayed()){ resumeSubscriptionButton.click(); } else {cancelSubscriptionButton.click();} return this; } 

I tried it like this:

 public ManageSubscriptionsTab hitResumeButtonIfDisplayed (boolean value ){ if (resumeSubscriptionButton.isDisplayed() == value){ resumeSubscriptionButton.click(); } else {cancelSubscriptionButton.click();} return this; } 

Essence, if there is no button on the page - press another button. But for some reason, the driver does not perform action, doesn’t perform, or rather, but not correctly. I can’t find the button (resumeSubscriptionButton), but according to the logic of the test, if it doesn’t exist, I need to press another button (cancelSubscriptionButton), but it doesn’t press it, but is still looking for the first one.

here for the test:

 @Test public void cancelSubscription (){ LoginPage loginPage = PageFactory.initElements(driver, LoginPage.class); ManageSubscriptionsTab manageSubscriptionsTab = PageFactory.initElements(driver, ManageSubscriptionsTab.class); loginPage.logInToTheAccount("",""); driver.get("https://.../manage-subscription"); manageSubscriptionsTab.hitResumeButtonIfDisplayed(); WebDriverWait wait = (new WebDriverWait(driver, 5)); wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[3]/div/div[2]/standard-form/form/div/div/button"))); manageSubscriptionsTab.clickSubmitCancellation(); Assert.assertTrue(manageSubscriptionsTab.yellowContainerIsDisplayed()); String textContainer = manageSubscriptionsTab.whenTheSubscriptionIsCanceledText(); Assert.assertEquals("Your subscription has been canceled. " + 

2 answers 2

 if (resumeSubscriptionButton.isDisplayed()){ 

It is logical that .isDisplayed() checks an existing object for visibility. And you need to check for availability in the DOM:

 if (resumeSubscriptionButton.isPresent()){ 
  • not quite clear. - Eugene Perm
  • The answer is supplemented. - Qwertiy
  • Thank you for pointing in the right direction! - Eugene Perm

In the end, I did this:

 public boolean resumeButtonIsDisplayed() { return resumeButton.size()>0 == true; } public ManageSubscriptionsTab hitResumeButtonifDisplayed ( ) { if (resumeButtonIsDisplayed()== true){ resumeSubscriptionButton.click(); } else { cancelSubscriptionButton.click(); } return this; } 

where true applies a list of items.