简体   繁体   中英

How to manage browser tabs with Serenity BDD?

How does one manage browser tabs or multiple browser windows with Serenity BDD?

For example I have a link in the site that I'm testing that opens a page in a different target, which means it opens in a new browser tab. My Serenity test can't locate a simple header (located by xpath "//h3") on the new page, and the only reason for this I can think of is that it's still looking at the original browser tab. So I would need to somehow switch the tab before making my assertion.

I know I can switch browser tabs at the web driver level. But since I can't get the driver from the actor directly, what would be the best way to go about accessing the driver an actor is using? Yeah, I'm providing that driver to the actor and therefore can keep a reference to it, but since I'm using multiple different actors in the same test (guest users causing changes that only an admin user can verify) I'd have to make it a bit more complex than just a simple web driver reference somewhere.

So my question, again, is: is there a nice way to manage the tabs in an actor's browser built into the Serenity BDD or screenplay pattern?

You could create an interaction class to switch tabs, and inside the interaction class, use the actor's driver directly, eg

public class SwitchToNewWindow implements Task {

    private static final Logger log = LoggerFactory.getLogger(SwitchToNewWindow.class);

    public SwitchToNewWindow() { }

    @Override
    public <T extends Actor> void performAs(T actor) {
        
        WebDriver driver = BrowseTheWeb.as(actor).getDriver();
        
        String currHandle = driver.getWindowHandle();
        Set<String> allHandles = driver.getWindowHandles();
        
        log.debug("open windows: " + allHandles.size());
        log.debug("current window: " + currHandle);
        for (String handle : allHandles) {
            if (!handle.contentEquals(currHandle)) {
                driver.switchTo().window(handle);
                break;
            }
        }
        log.debug("new window: " + driver.getWindowHandle());
    }

    public static SwitchToNewWindow change() {

        return Tasks.instrumented(SwitchToNewWindow.class);
    }
}
public class SwitchToNewWindow implements Task{
    public SwitchToNewWindow (){}
    
    public static SwitchToNewWindow switchToNewTab(){
       return Instrumented.instanceOf(SwitchToNewWindow.class).withProperties();
    }

@Step("{0} Switching to new Window")
    public <T extends Actor> void performAs(T actor) {
       String currentWindow = getDriver().getWindowHandle();
       Set<String> allWindows = getDriver().getWindowHandles();
       for(String window : allWindows){
         if(!window.contentEquels(currentWindow){
            getDriver().switchTo().window(window);
            break;
         }
       }
    }
}

In your Step Definitions

actor.attemptsTo(SwitchToNewWindow.switchToNewTab());
        actor.attemptsTo(Switch.toNewWindow());

如果它已经打开,则切换到新标签

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM