All Downloads are FREE. Search and download functionalities are using the official Maven repository.

im.yagni.driveby.driver.webdriver.WebDriverFactory.scala Maven / Gradle / Ivy

The newest version!
package im.yagni.driveby.driver.webdriver

import im.yagni.driveby.tracking.{BrowserOpenFailed, Tracker}
import org.openqa.selenium.{WebDriverException, WebDriver}
import java.io.File

//TODO: tidy up this complete and utter disgrace

//TIP: webdriver doesnt like too many creations at one ... so sleep a bit if it fails - see http://code.google.com/p/selenium/issues/detail?id=1402
object WebDriverFactory {
  val maxRetryAttempts = 5

  def ie: WebDriver = {
    import org.openqa.selenium.ie.InternetExplorerDriver
    import org.apache.http.conn.HttpHostConnectException

    var attempts = 0
    while (attempts < maxRetryAttempts) {
      try {
        return new InternetExplorerDriver
      }
      catch {
        case e: HttpHostConnectException =>
          Tracker.add(BrowserOpenFailed("ie"))
          Thread.sleep(3000)
          attempts += 1
      }
    }
    throw new RuntimeException("cant get here")
  }

  //TODO: support both firefoxPortable and firefoxPortableOptimised

  //TODO: need to pass location through
  def firefoxPortable: WebDriver = {
    import org.openqa.selenium.firefox.{FirefoxDriver, FirefoxProfile}

    sys.props += ("webdriver.firefox.bin" -> """.\tools\FirefoxPortable_7.0.1\App\Firefox\firefox.exe""")
    //    sys.props += ("webdriver.firefox.profile" -> "default")
    sys.props += ("webdriver.reap_profile" -> "true")
    //sys.props += ("webdriver.firefox.logfile" -> "webdriver_firefox.log")

    //TIP: to re-create webdriver extension
    //(1) run a test with:
    //- sys.props += ("webdriver.reap_profile" -> "false")
    //- new FirefoxDriver()
    //(2) copy contents of cygwin/tmp/anonymousXXXwebdriver-profile\extensions\[email protected] ...
    //... into /tools/FirefoxPortable_7.0.1/Data/profile/extensions/[email protected]
    //(3) trim out:
    //- linux stuff from 'platform'
    //(4) revert (1)

    //TOTRY:
    //put cygwin tmp dir on ram drive ... so profile gets copied there
    //optimise lift's js connections
    //zip up the profile so that .svn doesnt get copied make it twice as big as needed
    //find out if examples are being run in parallel
    //add one more test thread Runtime .. + 1

    //go after: org.apache.http.impl.client.DefaultRequestDirector tryConnect webdriver
    //[info]
    //15-Feb-2012 21:53:37 org.apache.http.impl.client.DefaultRequestDirector tryConnect
    //INFO: I/O exception (java.net.BindException) caught when connecting to the target host: Address already in use: connect
    //15-Feb-2012 21:53:37 org.apache.http.impl.client.DefaultRequestDirector tryConnect
    //INFO: Retrying connect

    //close down each browser in a new thread for speed
    //try spawn each browser in a new thread for speed (doubt it'll work)
    //remove support for firefoxLocalInstall and it's profile
    //see if args.store(never=true) improves speed as well

    //FAILED:
    //profileWithWebDriverAlreadyBendedIn.setPreference("webdriver.load.strategy", "unstable");
    //OK:
    //profileWithWebDriverAlreadyBendedIn.setPreference("webdriver.load.strategy", "fast");

    //TIP: optimise
    //http://code.google.com/p/selenium/wiki/FirefoxDriver
    //http://code.google.com/p/selenium/issues/detail?id=2164
    //http://code.google.com/p/selenium/wiki/TipsAndTricks

    var attempts = 0
    while (attempts < maxRetryAttempts) {
      try {
        val profileDir = new File(System.getProperty("user.dir") + "/tools/FirefoxPortable_7.0.1/Data/profile")

        val profileWithWebDriverAlreadyBendedIn = new FirefoxProfile(profileDir) {
          override protected def addWebDriverExtensionIfNeeded() {
          }
        }

        //temporarily off, just in case it's impacting reliability
        //profileWithWebDriverAlreadyBendedIn.setEnableNativeEvents(true)

        return new FirefoxDriver(profileWithWebDriverAlreadyBendedIn)
      }
      catch {
        case e: WebDriverException =>
          Tracker.add(BrowserOpenFailed("firefoxPortable"))
          Thread.sleep(3000)
          attempts += 1
      }
    }
    throw new RuntimeException("cant get here")
  }

  def firefox: WebDriver = {
    var attempts = 0
    while (attempts < maxRetryAttempts) {
      try {
        return new org.openqa.selenium.firefox.FirefoxDriver
      }
      catch {
        case e: WebDriverException =>
          Tracker.add(BrowserOpenFailed("firefox"))
          Thread.sleep(3000)
          attempts += 1
      }
    }
    throw new RuntimeException("cant get here")
  }

  //TODO: pass through BrowserVersion
  def htmlunit = {
    import org.openqa.selenium.htmlunit.HtmlUnitDriver
    import com.gargoylesoftware.htmlunit._

    new HtmlUnitDriver(BrowserVersion.INTERNET_EXPLORER_7) {
      getWebClient.setAjaxController(new NicelyResynchronizingAjaxController())

      override def get(url: String) {
        super.get(url)
        getWebClient.waitForBackgroundJavaScriptStartingBefore(10000)
      }
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy