Difficulties with concentration lead to examinations not going as planned

I'm currently running osx 10.9.2, protractor version 0.21.0, selenium-server-standalone version 2.40.0, and chromedriver version 2.9.

I have encountered some issues that I believe are related to a window focusing problem.

When I execute my end-to-end test using protractor, the browser window appears but my terminal remains in focus. This is evident from the fact that "Terminal" is displayed in my menu bar instead of "Chrome" (which indicates which application is active on OSX).

I attempted to resolve this by trying the following without success:

browser.driver.getAllWindowHandles().then(function(handles) {
  console.log(handles[0]);
  browser.driver.switchTo().window(handles[0]);
});

This issue results in certain tests failing. For instance, tests that involve clicking on a field with a bootstrap datepicker do not display the calendar, causing the test to be unable to interact with the datepicker.

The problem is exacerbated when using Firefox. In Firefox, dropdown menus do not appear when clicked if the browser is not in focus.

Interestingly, manually clicking on the browser window after it initially appears allows the tests to run normally.

Attempting the tests on a freshly installed Debian Linux system yielded similar behavior as described above.

Here are links to my configuration files: https://gist.github.com/giosakti/ca24a13705d15f4374b0

Answer №1

It can be tricky to ensure the correct order of window handlers in IE & Firefox, requiring iteration. Focus on new browser windows/tabs can also pose challenges.

To address these issues, I have created:

A function to help overcome these challenges

// Requires an element to verify the correct popup
var waitForPopUpHandle = function(elm, errorMessage) {
    if (errorMessage == null) {
        errorMessage = 'Expected a new browser tab or window to pop up';
    };
    if (elm == null) {
        throw 'waitForPopUpHandle must have an element to wait for!';
    };

    browser.ignoreSynchronization = true; // not a protractor page
    // IE & Firefox do not guarantee window handler order, so iteration is necessary.
    // Initially, wait for more than 1 browser tab
    browser.manage().timeouts().implicitlyWait(300); // a reasonable wait-retry time
    var i = 0;
    var popUpHandle = browser.driver.wait(function() {
        return browser.getAllWindowHandles().then(function(handles) {
            if (handles.length > 1) {
                return browser.switchTo().window(handles[i]).then(function() {
                    return browser.driver.isElementPresent(elm).then(function(result) {
                        if (result) {
                            return handles[i];
                        } else {
                            browser.sleep(400); // take a break
                            i = i + 1;
                            if (i >= handles.length) {
                                i = 0;
                            };
                            return false;
                        };
                    });
                });
            } else {
                browser.sleep(400); // take a break
                return false;
            };
        });
    }, browser.params.timeouts.pageLoadTimeout, errorMessage);
    // restore implicit wait
    browser.manage().timeouts().implicitlyWait(0); //restore

    return popUpHandle;
};

An example of using this function

var popUpHandle = waitForPopUpHandle(by.css('div.some-element-unique-to-that-popup'));
browser.switchTo().window(popUpHandle).then(function() {
    browser.ignoreSynchronization = true; // not an angular page
    browser.driver.findElement(by.css('div.some-element-unique-to-that-popup')); // wait for the elm
    // your expect()'s go here ...
    // ...
    browser.close().then(function() {
        // This close() promise is necessary on IE and probably on Firefox too
        var mainTab = waitForMainWindow();
        expect(browser.switchTo().window(mainTab).then(function() {
            browser.ignoreSynchronization = false; // restore if main window is an angular page
            // Ensure we are back on the main window
            // ....
            return true;
        })).toBe(true);
    });
});

Finally, the waitForMainWindow function

var waitForMainWindow = function(errorMessage) {
    if (errorMessage == null) {
        errorMessage = 'Expected main browser window to be available';
    };

    browser.ignoreSynchronization = true; // not an angular page
    return browser.driver.wait(function() {
        return browser.getAllWindowHandles().then(function(handles) {
            if (handles.length > 1) {
                var hnd = handles[handles.length - 1];
                return browser.switchTo().window(hnd).then(function() {
                    return browser.close().then(function() {
                        browser.sleep(400); // wait for close
                        return false;
                    });
                });
            } else {
                return handles[0];
            };
        });
    }, 5000, errorMessage);
};

Answer №2

After some troubleshooting, I managed to find a solution! By downgrading Chrome using the installer from this website, the focus issues disappeared (although they still persist on Firefox).

If you search "chrome 34 focus issues" on Google, you'll come across several reports that may be related to this problem. One example can be found at this link.

However, it's unclear whether the issue was a bug or expected behavior of Chrome 34. For now, I've decided to block Google Updater and stick with Chrome 33.

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

List-style-type outside of a table's boundaries

I recently experimented with using <ol> as list elements within a table in order to dynamically insert new table rows. <table> <thead> <tr> <th>head</th> <th>head</th> <th>h ...

Is there a way for blueimp to establish communication with a Java REST service?

$scope.$on('fileuploadadd', function(event, data) { $http({ method: 'POST', url: 'http://example.com/upload', data: data }).then(function successCallback(response){ console.log('File successfull ...

Filtering an array of JSONs in Vue.js using a separate array of JSONs

In my code, there are two arrays of JSONs: one named products and another called deletedProducts. The task is to filter out the products that are not present in the deletedProducts array. For instance: products = [ { id: 1, name: 'Box&apos ...

When I try to use Node.js and Express, I encounter an issue where I receive the

I recently developed a basic application. Everything was running smoothly until I decided to organize the code using an MVC template. However, now when you visit localhost:3000/add-service, you are greeted with a "Cannot Get /add-service" error message. W ...

How to Efficiently Organize OpenAI AI Responses Using TypeScript/JavaScript and CSS

I'm using a Next.js framework to connect to the OpenAI API, and I've integrated it seamlessly with an AI npm package. The functionality is incredible, but I've encountered an issue regarding line breaks in the responses. You can find the AI ...

Unexpected error in boot.ts file in Angular 2

I am currently experimenting with various folder arrangements for Angular 2. When attempting to launch a local server, I encounter the following error: Uncaught SyntaxError: Unexpected token < Evaluating http://localhost:3000/prod/app/TypeScript/bo ...

The function image.getState is not recognized (when trying to implement ol in an Angular project)

I attempted to integrate the code provided in an angular(4) application at the following link: However, I encountered an error every time I tried to launch the browser. To start with, here are my imports (both libraries were locally installed via npm): ...

Guidelines for toggling text visibility based on a randomly generated number with if statements in vanilla JavaScript

I am currently attempting to showcase some intricate text alongside an image once a button is clicked. At the moment, I have successfully implemented the functionality to display a random image when the button is clicked. However, I am encountering diffic ...

Refresh the Server Component once data has been modified by the Client Component within Next.js

I am still grappling with this specific scenario and trying to figure out the best approach in Next.js 13. Can anyone provide guidance on the correct way to handle this? In a Server Component, I have a list of users being displayed, leveraging MongoDB, as ...

Unable to invoke method because ViewChild component is undefined

I am currently fetching data from a database and once the data is ready, I want to bind it to a child element using ViewChild in order to pass the object data to the child component. The problem I'm facing is that the component is not immediately "re ...

The checkbox click function is not functioning properly when placed within a clickable column

In my coding project, I decided to create a table with checkboxes in each column. <table class="bordered"> <thead> <tr style="cursor:pointer" id="tableheading" > <th>Name ...

Learn how to retrieve data using the $.ajax() function in jQuery and effectively showcase it on your HTML page

Can someone assist me with extracting data from https://jsonplaceholder.typicode.com/? Below is the AJAX call I'm using: $.ajax({ url: root + '/posts/', data: { userId: 1 }, type: "GET", dataType: "json", success: function(data) { ...

Utilizing Angular 2's offline capabilities for loading locally stored JSON files in a project folder

I've been attempting to load a local JSON file from my Angular 2 project folder using the HTTP GET method. Here is an example of the code snippet: private _productURL = 'api/products/products.json'; getProducts(): Observable<any> ...

A reliable approach for dynamically altering SVG graphics

It seems like IE10 does not support CSS transformations for SVGs, only attribute transformations. Here is an example: <svg><rect id="myrect" width="200" height="200"></rect></svg> setTimeout(function() { var r = document.getE ...

Is there a way to programmatically trigger the opening of the Inspect Element tool (F12) in

Is there a way to access the network tab directly from my code in Selenium WebDriver using Java? Alternatively, is there a method to simulate pressing the F12 button through script? ...

React 15 is found to be incompatible with React-redux due to certain compatibility

I'm currently working on upgrading to the newest version of [email protected] in my project, which also utilizes the react-redux@^4.4.0 package. However, I encountered issues when attempting to follow the upgrade instructions provided in the documenta ...

What is the process of sending data to another JSP page via AJAX when submitting data to a link within a JSP page?

I am facing an issue with forwarding data from a URL link in a JSP form page to another JSP page after submitting the form. How can I achieve this successfully? index.jsp <%@page contentType="text/html" pageEncoding="UTF-8"%> <!DOCTYPE html> ...

Is it possible to include if else logic code within a catch statement?

There's a nagging feeling within me that having logic code within a catch statement is not the right approach. For example, my catch block currently looks like this: try{ // do some stuff that throws some unexpected errors } ...

Utilize Ajax to open and close an HTML tag within separate div elements

I am facing a challenge in my project where I have to dynamically open and close a form using ajax. The form needs to be opened within a div, and then closed in another div below it like the following: Opening form (header div) $('#header').h ...

The solution to the issue: [splide] Unable to find a track/list element

Currently, I am trying to create a thumbnail carousel following this tutorial, but I seem to be encountering an error. The guide does not provide instructions on how to create the #main-carousel element, so despite trying different methods, I am still unab ...