How to stop automatically closing browser when wri

2019-03-18 15:31发布

问题:

I am new to writing test cases using protractor for non angular application. I wrote a sample test case.Here the browser closes automatically after running test case.How can I prevent this. Here is my code

var submitBtnElm = $('input[data-behavior=saveContribution]');

      it('Should Search', function() {
        browser.driver.get('http://localhost/enrollments/osda1.html');   
        browser.driver.findElement(by.id('contributePercentValue')).sendKeys(50);
        submitBtnElm.click().then(function() {

        });
      });

回答1:

Add browser.pause() at the end of your it function. Within the function itself.



回答2:

I suggest you to use browser.driver.sleep(500); before your click operation.

See this.

browser.driver.sleep(500);

element(by.css('your button')).click();
browser.driver.sleep(500);


回答3:

Add a callback function in It block and the browser window doesn't close until you call it. So perform the action that you need and place the callback at your convenience

var submitBtnElm = $('input[data-behavior=saveContribution]');
  it('Should Search', function(callback) {
    browser.driver.get('http://localhost/enrollments/osda1.html');   
    browser.driver.findElement(by.id('contributePercentValue')).sendKeys(50);
    submitBtnElm.click().then(function() {
       // Have all the logic you need
       // Then invoke callback
       callback();
    });
  });


回答4:

The best way to make browser NOT to close for some time, Use browser.wait(). Inside the wait function write logic for checking either visibilityOf() or invisibilityOf() of an element, which is not visible or it will take time to become invisible on UI. In this case wait() keep on checking the logic until either condition met or timeout reached. You can increase the timeout if you want browser visible more time.

var EC=protractor.ExpectedConditions;
var submitBtnElm = $('input[data-behavior=saveContribution]');

it('Should Search', function() {
   browser.driver.get('http://localhost/enrollments/osda1.html');   
   browser.driver.findElement(by.id('contributePercentValue')).sendKeys(50);
   submitBtnElm.click().then(function() {
   browser.wait(function(){
            EC.invisibilityOf(submitBtnElm).call().then(function(isPresent){
                if(isPresent){
                    return true;
                }
            });
        },20000,'error message');
    });
  });


回答5:

I was also struggling with a similar issue where i had a test case flow where we were interacting with multiple application and when using Protractor the browser was closing after executing one conf.js file. Now when I looked into the previous response it was like adding delay which depends on how quick your next action i performed or it was hit or miss case. Even if we think from debugging perspective most of the user would be performing overnight runs and they would want to have browser active for couple of hours before they analyze the issue. So I started looking into the protractor base code and came across a generic solution which can circumvent this issue, independent of any browser. Currently the solution is specific to requirement that browser should not close after one conf.js file is executed, then could be improved if someone could add a config parameter asking the user whether they want to close the browser after their run.

The browser could be reused for future conf.js file run by using tag --seleniumSessionId in command line.

Solution:

  • Go to ..\AppData\Roaming\npm\node_modules\protractor\built where your protractor is installed.
  • Open driverProvider.js file and go to function quitDriver
  • Replace return driver.quit() by return 0

As far as my current usage there seems to be no side effect of the code change, will update if I came across any other issue due to this change. Snapshot of code snippet below.

Thanks Gleeson

Snapshot of code snippet:



回答6:

I'm sure there is a change triggered on your page by the button click. It might be something as subtle as a class change on an element or as obvious as a <p></p> element with the text "Saved" displayed. What I would do is, after the test, explicitly wait for this change.

[...]
return protractor.browser.wait(function() {
    return element(by.cssContainingText('p', 'Saved')).isPresent();
}, 10000);

You could add such a wait mechanism to the afterEach() method of your spec file, so that your tests are separated even without the Protractor Angular implicit waits.



回答7:

var submitBtnElm = $('input[data-behavior=saveContribution]');

  it('Should Search', function() {
    browser.driver.get('http://localhost/enrollments/osda1.html');   
    browser.driver.findElement(by.id('contributePercentValue')).sendKeys(50);
    submitBtnElm.click().then(function() {

    });
    browser.pause(); // it should leave browser alive after test
  });

browser.pause() should leave browser alive until you let it go.

@Edit Another approach is to set browser.ignoreSynchronization = true before browser.get(...). Protractor wouldn't wait for Angular loaded and you could use usual element(...) syntax.



回答8:

I found Gleeson's solution is working, and that really helped me. The solution was...

  1. Go to %APPDATA%Roaming\npm\node_modules\protractor\built\driverProviders\
  2. Find driverProviders.js
  3. Open it in notepad or any other text editor
  4. Find and Replace return driver.Quit() to return 0
  5. Save the file

Restart your tests after that.

I am using node v8.12.0 npm v6.4.1 protractor v5.4.1

This solution will work, only if you installed npm or protractor globally; if you have installed your npm or protractor locally (in your folder) then, you have to go to your local protractor folder and do the same.



标签: protractor