2015-10-18 67 views
0

我一直試圖通過WebBrowser作爲Selenium ScalaTest Spec中的隱式參數,但它失敗。我對所有的試驗基地超:這是隱式參數 - 找不到隱式值參數

abstract class BaseSpec extends FunSpec with WebBrowser with ShouldMatchers { 
    implicit val webDriver: WebDriver = new FirefoxDriver 
} 

那我也有與內隱WebBrowser參數的方法Page對象:

object LoginPage extends Page { 
    def login(username: String, password: String) (implicit browser : WebBrowser) = {  
    //... 
    } 

然後,我想打電話從實際的login方法規範。作爲規範類通過其BaseSpec超實現WebBrowser特質我希望規範實例調用該方法被解析爲隱式web瀏覽器:

class LoginSpec extends BaseSpec { 

    it("Should fail on invalid password") { 
    LoginPage login("wrong username", "wrong password") 
    assertIsOnLoginPage() 
    } 
} 

但這種失敗,編譯錯誤:

could not find implicit value for parameter browser: org.scalatest.selenium.WebBrowser

上line LoginPage login("wrong username", "wrong password")

我是否總是需要明確提供this作爲WebBrowser參數值還是有更好的辦法嗎?

+0

https://stackoverflow.com/questions/4269572/is-it-possible-to-通過此結果作爲隱參數合階 – tuxdna

回答

1

As the spec class implements the WebBrowser trait via its BaseSpec superclass I expect the spec instance calling the method to be resolved as the implicit WebBrowser

this不能作爲一個隱含的自動,但您可以輕鬆添加:

abstract class BaseSpec extends FunSpec with WebBrowser with ShouldMatchers { 
    implicit def webBrowser: WebBrowser = this 
    implicit val webDriver: WebDriver = new FirefoxDriver 
} 
1

創建內部class LoginSpec一個隱含的VAL在此代碼:

trait WebBrowser 
class WebDriver 
class FunSpec 
trait ShouldMatchers 
class FirefoxDriver extends WebDriver 

abstract class BaseSpec extends FunSpec with WebBrowser with ShouldMatchers { 
    implicit val webDriver: WebDriver = new FirefoxDriver 
} 

trait Page 

object LoginPage extends Page { 
    def login(username: String, password: String)(implicit browser: WebBrowser) = { 
    println(username, password) 
    } 
} 

class LoginSpec extends BaseSpec { 
    implicit val browser: WebBrowser = this 
    def fun = { 
    LoginPage login("wrong username", "wrong password") 
    } 
} 

object ImplicitThis { 
    def main(args: Array[String]) { 
    new LoginSpec().fun 
    } 
} 

這編譯和工作得很好。