Rajive Pai
Rajive Pai

Reputation: 312

Is there a way to use Protractor and Selenium together?

I have an application under test, that has an iFrame, where Angular app is injected. My test suite is in Java Selenium and I am finding it very difficult to manipulate the controls inside the angular app.

I have used protractor and selenium in isolation, but this scenario, I am finding it difficult to get a solution.

My question is:

  1. Is there a way that I can wait for angular load completely before I can switch to the iFrame and manipulate the controls purely in selenium?
  2. If not, is there a way or any one has any work around where only for angular app part we can use the Protractor scripts? (Basically I am asking if protractor and selenium suites can go hand in hand?)

Any help greatly appreciated.

Upvotes: 1

Views: 1025

Answers (1)

Vishal Aggarwal
Vishal Aggarwal

Reputation: 4178

We are also automating Angularjs Application using Selenium & java. We have written our own sync and wait for load functions as below:

public void Sync()
        {
            //waitForLoading();
            try
            {
             JavascriptExecutor js = (JavascriptExecutor)driver;   
             WebDriverWait wait = new WebDriverWait(webDriver, 300);//timeoutInSeconds
             wait.Until(js.executeScript("return document.readyState").toString().equals("complete"));
                Thread.sleep(4000);

            }
            catch
            {
                Thread.sleep(16000);
            }
        }


//Another version of the same function based on time based polling to make it more dynamic
public void checkPageIsReady() {

  JavascriptExecutor js = (JavascriptExecutor)driver;


  //Initially bellow given if condition will check ready state of page.
  if (js.executeScript("return document.readyState").toString().equals("complete")){ 
   System.out.println("Page Is loaded.");
   return; 
  } 

  //This loop will rotate for 25 times to check If page Is ready after every 1 second.
  //You can replace your value with 25 If you wants to Increase or decrease wait time.
  for (int i=0; i<25; i++){ 
   try {
    Thread.sleep(1000);
    }catch (InterruptedException e) {} 
   //To check page ready state.
   if (js.executeScript("return document.readyState").toString().equals("complete")){ 
    break; 
   }   
  }
}

Upvotes: 1

Related Questions