serenity-bdd / serenity-core

Serenity BDD is a test automation library designed to make writing automated acceptance tests easier, and more fun.
http://serenity-bdd.info
Other
722 stars 518 forks source link

switchToPage is deprecated. What should i use instead? #2131

Closed v1-wizard closed 2 years ago

v1-wizard commented 4 years ago

Hi, i just decided to migrate from old Serenity-BDD version to new one. I found that switchToPage is deprecated. What should i use instead?

I also have a little proposal: could you create a comments with migration advises from deprecated methods in future?

Thanks. Have a nice day.

ahmed-al-sadoon commented 4 years ago

Same with me, i couldn't finde any reference on Internet

wakaleo commented 4 years ago

It looks like people are still using this method so I will un-deprecate it :-).

ahmed-al-sadoon commented 4 years ago

@wakaleo Thanks a lot :)

mi-akram commented 3 years ago

Has this been fixed? Seems like this is still an issue on version 2.3.4.

I have also checked this on version 2.3.12 and seems to be broken on this version too. I seem to be getting a NullPointerException when this is used:

java.lang.NullPointerException at net.serenitybdd.core.pages.PageObject.switchToPage(PageObject.java:210)

wakaleo commented 3 years ago

No fix because it's intended to be deprecated.

HajekOndrej commented 3 years ago

No fix because it's intended to be deprecated.

Okay, but then the original question stands:) What should we use instead?

wakaleo commented 3 years ago

You don't need to use anything. Just use the page object (or locators) you want to use. What problem are you trying to solve?

HajekOndrej commented 3 years ago

You don't need to use anything. Just use the page object (or locators) you want to use. What problem are you trying to solve?

Right, I thought that may be the that. Okay, thanks :)

Neetish1607 commented 3 years ago

@wakaleo : I am using this as a workaround ! return pages.getPage(.class); for serenity : 2.3.12 version and its working fine for me !

wakaleo commented 3 years ago

You can do that. But why do you need to return anything at all? Why not just use the page you expect to be displayed, rather than trying to bind your code to the structure of the application UI?

Neetish1607 commented 3 years ago

with you on that !! I just thought that some people were looking for solution ! Had one so put it here !! On a separate note I have one query : Trying to Run UI Tests on Jenkins on my Local with Chrome version 89 and serenity : 2.3.12! Its not opening the Browser and its getting stuck right here : Starting ChromeDriver 89.0.4389.23 (61b08ee2c50024bab004e48d2b1b083cdbdac579-refs/branch-heads/4389@{#294}) on port 16121 Only local connections are allowed. Please see https://chromedriver.chromium.org/security-considerations for suggestions on keeping ChromeDriver safe. ChromeDriver was started successfully.

Its gets stuck here for a long period of time and eventually i have to stop the jenkins run ! Running on windows my serenity.properties looks like this :

webdriver.driver=chrome serenity.project.name=Demo project webdriver.wait.for.timeout=10000 wedriver.timeouts.implicitlywait=5000 serenity.take.screenshots=FOR_EACH_ACTION webdriver.base.url=http://localhost:8099 chrome.capabilities.acceptSslCerts = true chrome.capabilities.handlesAlerts = true chrome.switches=--allowed-ips

serenity.driver.capabilities =

serenity.use.unique.browser = false

And Serenity.conf looks like this : drivers { windows { webdriver.chrome.driver = src/test/resources/drivers/windows/chromedriver.exe webdriver.gecko.driver = src/test/resources/drivers/windows/geckodriver.exe webdriver.ie.driver = src/test/resources/drivers/windows/IEDriverServer.exe phantomjs.binary.path = src/test/resources/drivers/windows/phantomjs.exe webdriver.edge.driver = src/test/resources/drivers/windows/MicrosoftWebDriver.exe } mac { webdriver.chrome.driver = src/test/resources/drivers/mac/chromedriver webdriver.gecko.driver = src/test/resources/drivers/mac/geckodriver phantomjs.binary.path = src/test/resources/drivers/mac/phantomjs } linux { webdriver.chrome.driver = src/test/resources/drivers/linux/chromedriver webdriver.gecko.driver = src/test/resources/drivers/linux/geckodriver } }

Unable to find where is the gap !! Can you please help @wakaleo ?

gigos85 commented 3 years ago

You can do that. But why do you need to return anything at all? Why not just use the page you expect to be displayed, rather than trying to bind your code to the structure of the application UI?

Hello, how do you manage the fact that you go from one page to another without the switchtopage or just open the destination page ? have you an example of page switching ?

Thanks for your help

wakaleo commented 3 years ago

Modeling navigation in your tests is a bit of an anti-pattern - what if you go to a different page? Just use the page that you expect to be visible.

gigos85 commented 3 years ago

Hello,

First of all, thanks for your reply. We are aligned with the fact that it's probably risky (with lik your test with sut). But in some case to validate behavior we need to go through more than one page object (more than one url). In this case, what is the best practice (and have you some examples).

thanks,

wakaleo commented 3 years ago

Just because you need to navigate through page objects doesn't mean your test automation code needs to hard-code this navigation. Consider the following steps (using the Action Classes pattern:

@Steps 
LoginSteps login;

@Steps  
NavigationSteps navigate;

@Steps 
ProductListSteps inTheProductList;

login.withValidCredentials()
navigate.to(TodaysDiscounts)
inTheProductList.selectTheFirstItem()
...

Each method will then access the UI elements it needs to do its job. You can group related UI locators into classes and call these Page Objects if you like. But you don't need to hard-code any navigation logic in your test code. For example:

class ProductListSteps extends UIInteractionSteps {

   @Step 
  public void selectTheFirstItem() {
    $(ProductList.FIRST_VISIBLE_ITEM).click();
  }

}
...
class ProductList {
    static By FIRST_VISIBLE_ITEM = By.cssSelector("...");
}
gigos85 commented 3 years ago

hello,

Thanks for you answer and your help.