navigate back to the previous tab using protractor

When I open a new tab (second), I attempt to switch back to the first tab.

     common.clickOpenNewSession(); //opens a new tab

 browser.getAllWindowHandles().then(function (handles) {
    var secondWindowHandle = handles[1];
    var firstWindowHandle = handles[0];
    browser.switchTo().window(secondWindowHandle).then(function () { //focus shifts to new tab
        browser.sleep(3000);
        expect(browser.driver.getCurrentUrl()).toContain("url");
//perform some actions

    });
//the following does not work. I try to return to the previous tab without closing the second tab
    browser.actions().sendKeys(protractor.Key.CONTROL).sendKeys(protractor.Key.TAB).perform();
    browser.sleep(4000);
    browser.driver.switchTo().window(firstWindowHandle);
    browser.setLocation('http://google.com');
});

Answer №1

One issue you may be encountering is attempting to navigate back to the previous tab using ctrl + tab, rather than utilizing window handles for navigation. This approach is not compatible with WebDriver as ctrl + tab is a system-level function that cannot be replicated across different operating systems and browsers. To resolve this, simply employ browser.switchTo() once more.

Answer №2

@Sarah This script retrieves all available browser handles and fulfills the promise. It then opens a new tab created by a

<a href='newlink' target='_blank'>Link</a>
:

POFile.buttonWithABlankTarget.click().then(function() {
    browser.getAllWindowHandles().then(function(handles) {
        var newTabHandle = handles[1];
        browser.switchTo().window(newTabHandle).then(function () {
            // Verify that the newly opened tab URL matches the invite href
            expect(browser.driver.getCurrentUrl()).toContain("http://...");
        });
    });
});

You can also switch between tabs with similar logic:

it("should activate the first tab", function() {
    browser.getAllWindowHandles().then(function (handles) {
        browser.switchTo().window(handles[0]);
    });
});

Additionally, you can close a tab:

it("should close the second tab and return to the first tab", function() {
    browser.getAllWindowHandles().then(function (handles) {
        // Currently on the second tab...
        browser.driver.close();
        browser.switchTo().window(handles[0]);
    });
});

I hope this information is useful!

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Having trouble with enabling HTML5 mode to true on an Angular frontend and Laravel backend

I successfully removed the "#!" from my website URL and everything is working perfectly. The only issue I am facing is that when I try to reload a sub URL other than the home page, it shows a "page not found" error. However, I am able to access the same s ...

Using Selenium's findElement method along with By.cssSelector to locate elements with multiple classes

I'm a beginner with selenium. Can someone explain how to locate an element that has multiple classes and values? <div class="md-tile-text--primary md-text">Getting Started</div> using the findElement method By .css Selector. ...

Struggling with expanding a Javascript object? Let us assist you!

Imagine having a JavaScript object... app.widget = {} (function (W) { W.superFunction = function() { console.log("Hey!"); } ...other functions... })(app.widget) Now, let's create a clone of this object... app.specialWidget ...

How to retrieve a subobject using AngularJS

From my perspective : <span data-ng-init="fillEditForm['titi']='toto'" ></span> In my Angular controller : console.log($scope.fillEditForm); console.log($scope.fillEditForm['titi']); The outcome : Object { ti ...

iOS Chrome: Enabling Cookies with "Always Allow"

While the Safari browser on OSX has a setting under Privacy & Security -> Block Cookies -> Always Allow, enabling the storage of entries in the browser's local storage even when accessing pages from third party sites like those running in an iframe, I ...

Incorporating middleware in Next.js to remove route protection

Looking to remove the protection for the login page, and here is how my folder structure looks: https://i.stack.imgur.com/klPYV.png This is the middleware I am using: import { NextResponse, NextRequest } from "next/server"; export async functi ...

Guide to developing a manual tally counter with recorded logs using HTML and JavaScript

I am currently in need of assistance with creating a manual input counter using the App Script Editor. My website design already includes a single input textbox, a reset button, and a disabled box. What I would like to achieve is that when I enter a numb ...

Tips for expanding the content of a blogger page to fill the entire frame of the page

Check out this page: . Currently, the video on the page does not fill up the entire screen. Any suggestions for a solution? ...

Error encountered due to an unhandled promise rejection of type

I am currently using async/await to execute a query to the database and receive the result. However, I encountered an error in the browser console that says: Unhandled promise rejection TypeError: "games is undefined" In my code, there are two function ...

"Exploring the World Wide Web with Internet Explorer Driver and Webdriver

After trying everything I know to make internet explorer work with webdriver.io, I've encountered a confusing issue. To start, download the internet explorer driver from this link: http://www.seleniumhq.org/download/. The file is an .exe named ' ...

I could use some assistance with deciphering JSON data

After using console.log to display the data I received, I observed an object structured as follows (I trimmed some details for clarity and used ... to indicate repetitive information): [ Submission { title: 'Untitled', content: { ur ...

Ways to solely cache spa.html using networkfirst or ways to set up offline mode with server-side rendering (SSR)

I am facing an issue with my application that has server-side rendering. It seems like the page always displays correctly when there is an internet connection. However, I am unsure how to make Workbox serve spa.html only when there is no network available. ...

Is it possible to exclusively focus on the specified data attributes?

Is there a method to specifically target the 'data-season' attribute with the value "winter"? This is my HTML: <li data-season="summer">summer <ul class="groups"> <li data-item="beach">beach</li> <li data-item ...

What is the best way to create a button with this functionality?

In the form that I have created, it is opened in a bootstrap modal style. This form contains a button that, when clicked, triggers an alert box to appear. The code snippet used for this functionality is as follows: echo "<script>"; echo "alert(&apos ...

What is the best way to pass multiple parameters along with the context to a URL in Vuex?

Link to StackBlitz I am currently working on how to send multiple action parameters with context to a URL. I have been encountering a 400 error in my attempts. The selectedPoint and departurePoint are coming from child components and stored as variables i ...

Leverage the power of Laravel and ReactJS to beautifully display images

Currently, I am working on a project where I am incorporating ReactJS into a Laravel blade file. To get started, I included the React CDN and began writing code within the script tag. Everything seems to be going smoothly so far, however, I have encountere ...

Having trouble retrieving an excel sheet using a User-Defined Function (UDF) in Selenium WebDriver

I am currently learning about User Defined Functions (UDF) within a hybrid framework. I have developed an "ExcelUtils" class where I wrote code to read and write from an excel sheet. Additionally, I created a "Utils" class in which I encountered an issue w ...

Utilize Google Home to browse through nearby websites and respond to inquiries by incorporating Javascript

Can Google Home be programmed to read from a local webpage and provide answers based on the content of that page through Javascript? ...

Importing models in SceneJS does not function properly with Internet Explorer

I've been exploring different webGL frameworks lately. While I like SceneJS, I've noticed some compatibility issues with Internet Explorer. For instance, in IE 11, importing OBJ files seems to cause the online examples to freeze up: Check out th ...

Utilize select2 for dynamically loading an external html component

When the page is loaded, it includes another HTML page with a select element that I want to style using select2. The basic page structure looks like this: <select class="selectAddItem" id="selectAddItem" name="selectAddItem" style="width: 150px;" clas ...