如何使用play 2框架的phantomjs设置视口宽度

时间:2014-06-09 10:49:33

标签: scala playframework-2.0 phantomjs

我一直在努力解决这个问题,我找不到一种方法来指导phantomjs关于视口。我正在使用play 2.2(代码基于:Set Accept-Language on PhantomJSDriver in a Play Framework Specification

package selenium

import org.specs2.mutable.Around
import org.specs2.specification.Scope
import play.api.test.{TestServer, TestBrowser, FakeApplication}
import org.openqa.selenium.remote.DesiredCapabilities
import org.specs2.execute.{Result, AsResult}
import play.api.test.Helpers._
import scala.Some
import org.openqa.selenium.phantomjs.PhantomJSDriver
import org.openqa.selenium.NoSuchElementException
import java.io.File
import java.io.PrintWriter
import java.util.concurrent.TimeUnit
import collection.JavaConversions._

abstract class WithPhantomJS(val additionalOptions: Map[String, String] = Map()) extends Around with Scope {

    implicit def app = FakeApplication()
    implicit def port = play.api.test.Helpers.testServerPort

    // for phantomjs
    lazy val browser: TestBrowser = {
        val defaultCapabilities = DesiredCapabilities.phantomjs

        print(defaultCapabilities.toString)

        val additionalCapabilities = new DesiredCapabilities(mapAsJavaMap(additionalOptions))
        val capabilities = new DesiredCapabilities(defaultCapabilities, additionalCapabilities)


        val driver = new PhantomJSDriver(capabilities)

        val br = TestBrowser(driver, Some("http://localhost:" + port))



        br.webDriver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS)
        br
    }

    override def around[T: AsResult](body: => T):Result = {
        try {
            running(TestServer(port, FakeApplication()))(AsResult.effectively(body))

        } catch {
            case e: Exception => {
                val fn: String = "/tmp/test_" + e.getClass.getCanonicalName;

                browser.takeScreenShot(fn + ".png")
                val out = new PrintWriter(new File(fn + ".html"), "UTF-8");
                    try {
                    out.print(browser.pageSource())
                } finally {
                    out.close
                }
                throw e;
            }
        } finally {
            browser.quit()
        }

    }
}

,实际测试如下:

class ChangeName extends Specification {

  "User" should {

    "be able to change his name in account settings" in new WithPhantomJS() {

    // ... this throws

我想要渲染网站的异常处理程序,但是,我可以看到匹配移动设备(窄)。

如何更改宽度?

1 个答案:

答案 0 :(得分:1)

这似乎有效:

import org.openqa.selenium.Dimension
...

browser.webDriver.manage().window().setSize(new Dimension(1280, 800))