Naftoli Gugenheim | 31 May 08:52 2011
Picon

[Lift] Selenium 2 mini-DSL

To write tests for a web application, I seem to have ended up going with Selenium 2 via ScalaTest. I wrote a mini DSL:

  import driver._
  def finder(c: Char): String => WebElement = s => findElement(c match {
    case '#' => By id s
    case '.' => By className s
    case '$' => By cssSelector s
    case '%' => By xpath s
    case ':' => By name s
    case '=' => By linkText s
    case '~' => By partialLinkText s
    case _ => By tagName c + s
  })
  implicit def str2el(s: String): WebElement =
    s.toList match { case c :: cs => finder(c)(cs.mkString) }


So now I can write tests like this:

    "=Check Out" click()
    "#phone" sendKeys ("2222222222", Keys.ENTER)
    ".firstname".getValue should equal("TestPersonFirst")


Enjoy.

--
You received this message because you are subscribed to the Google Groups "Lift" group.
To post to this group, send email to liftweb-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To unsubscribe from this group, send email to liftweb+unsubscribe <at> googlegroups.com.
For more options, visit this group at http://groups.google.com/group/liftweb?hl=en.
David Pollak | 31 May 18:43 2011
Picon

Re: [Lift] Selenium 2 mini-DSL

Niiice!

On Mon, May 30, 2011 at 11:52 PM, Naftoli Gugenheim <naftoligug-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:
To write tests for a web application, I seem to have ended up going with Selenium 2 via ScalaTest. I wrote a mini DSL:

  import driver._
  def finder(c: Char): String => WebElement = s => findElement(c match {
    case '#' => By id s
    case '.' => By className s
    case '$' => By cssSelector s
    case '%' => By xpath s
    case ':' => By name s
    case '=' => By linkText s
    case '~' => By partialLinkText s
    case _ => By tagName c + s
  })
  implicit def str2el(s: String): WebElement =
    s.toList match { case c :: cs => finder(c)(cs.mkString) }


So now I can write tests like this:

    "=Check Out" click()
    "#phone" sendKeys ("2222222222", Keys.ENTER)
    ".firstname".getValue should equal("TestPersonFirst")


Enjoy.

--
You received this message because you are subscribed to the Google Groups "Lift" group.
To post to this group, send email to liftweb-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org.
To unsubscribe from this group, send email to liftweb+unsubscribe <at> googlegroups.com.
For more options, visit this group at http://groups.google.com/group/liftweb?hl=en.



--
Lift, the simply functional web framework http://liftweb.net

--
You received this message because you are subscribed to the Google Groups "Lift" group.
To post to this group, send email to liftweb-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To unsubscribe from this group, send email to liftweb+unsubscribe <at> googlegroups.com.
For more options, visit this group at http://groups.google.com/group/liftweb?hl=en.
Naftoli Gugenheim | 31 May 20:51 2011
Picon

Re: [Lift] Selenium 2 mini-DSL

By the way, that's instead of writing (assuming driver._ is imported)

findElement(By linkText "Check Out") click()
findElement(By id "phone") sendKeys ("2222222222", Keys.ENTER)
findElement(By className "firstname").getValue should equal("TestPersonFirst")



On Tue, May 31, 2011 at 12:43 PM, David Pollak <feeder.of.the.bears-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:
Niiice!

On Mon, May 30, 2011 at 11:52 PM, Naftoli Gugenheim <naftoligug-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:
To write tests for a web application, I seem to have ended up going with Selenium 2 via ScalaTest. I wrote a mini DSL:

  import driver._
  def finder(c: Char): String => WebElement = s => findElement(c match {
    case '#' => By id s
    case '.' => By className s
    case '$' => By cssSelector s
    case '%' => By xpath s
    case ':' => By name s
    case '=' => By linkText s
    case '~' => By partialLinkText s
    case _ => By tagName c + s
  })
  implicit def str2el(s: String): WebElement =
    s.toList match { case c :: cs => finder(c)(cs.mkString) }


So now I can write tests like this:

    "=Check Out" click()
    "#phone" sendKeys ("2222222222", Keys.ENTER)
    ".firstname".getValue should equal("TestPersonFirst")


Enjoy.

--
You received this message because you are subscribed to the Google Groups "Lift" group.
To post to this group, send email to liftweb-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org.
To unsubscribe from this group, send email to liftweb+unsubscribe <at> googlegroups.com.
For more options, visit this group at http://groups.google.com/group/liftweb?hl=en.



--
Lift, the simply functional web framework http://liftweb.net

--
You received this message because you are subscribed to the Google Groups "Lift" group.
To post to this group, send email to liftweb-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org.
To unsubscribe from this group, send email to liftweb+unsubscribe <at> googlegroups.com.
For more options, visit this group at http://groups.google.com/group/liftweb?hl=en.

--
You received this message because you are subscribed to the Google Groups "Lift" group.
To post to this group, send email to liftweb-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To unsubscribe from this group, send email to liftweb+unsubscribe <at> googlegroups.com.
For more options, visit this group at http://groups.google.com/group/liftweb?hl=en.
Viktor Hedefalk | 31 May 22:24 2011
Picon

Ang.: Re: [Lift] Selenium 2 mini-DSL

Really sweet! Now I have no more excuses for not writing those web tests.

--
You received this message because you are subscribed to the Google Groups "Lift" group.
To post to this group, send email to liftweb-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To unsubscribe from this group, send email to liftweb+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
For more options, visit this group at http://groups.google.com/group/liftweb?hl=en.
Diego Medina | 1 Jun 04:35 2011

Re: [Lift] Selenium 2 mini-DSL

Very cool, thanks for sharing.

At work our main app is written on java and we use selenium for
integration testing, writing a DSL to simplify some tests may be
another way to sneak in some scala in our team :)

  Diego

On Tue, May 31, 2011 at 2:52 AM, Naftoli Gugenheim
<naftoligug@...> wrote:
> To write tests for a web application, I seem to have ended up going with
> Selenium 2 via ScalaTest. I wrote a mini DSL:
>   import driver._
>   def finder(c: Char): String => WebElement = s => findElement(c match {
>     case '#' => By id s
>     case '.' => By className s
>     case '$' => By cssSelector s
>     case '%' => By xpath s
>     case ':' => By name s
>     case '=' => By linkText s
>     case '~' => By partialLinkText s
>     case _ => By tagName c + s
>   })
>   implicit def str2el(s: String): WebElement =
>     s.toList match { case c :: cs => finder(c)(cs.mkString) }
>
> So now I can write tests like this:
>     "=Check Out" click()
>     "#phone" sendKeys ("2222222222", Keys.ENTER)
>     ".firstname".getValue should equal("TestPersonFirst")
>
> Enjoy.
>
> --
> You received this message because you are subscribed to the Google Groups
> "Lift" group.
> To post to this group, send email to liftweb@...
> To unsubscribe from this group, send email to
> liftweb+unsubscribe@...
> For more options, visit this group at
> http://groups.google.com/group/liftweb?hl=en.
>

-- 
Diego Medina
Web Developer
http://www.fmpwizard.com

--

-- 
You received this message because you are subscribed to the Google Groups "Lift" group.
To post to this group, send email to liftweb@...
To unsubscribe from this group, send email to liftweb+unsubscribe <at> googlegroups.com.
For more options, visit this group at http://groups.google.com/group/liftweb?hl=en.

Paul Dale | 1 Jun 17:37 2011
Picon

Re: [Lift] Selenium 2 mini-DSL

Very cool.

On Tue, May 31, 2011 at 8:52 AM, Naftoli Gugenheim <naftoligug-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:
To write tests for a web application, I seem to have ended up going with Selenium 2 via ScalaTest. I wrote a mini DSL:

  import driver._
  def finder(c: Char): String => WebElement = s => findElement(c match {
    case '#' => By id s
    case '.' => By className s
    case '$' => By cssSelector s
    case '%' => By xpath s
    case ':' => By name s
    case '=' => By linkText s
    case '~' => By partialLinkText s
    case _ => By tagName c + s
  })
  implicit def str2el(s: String): WebElement =
    s.toList match { case c :: cs => finder(c)(cs.mkString) }


So now I can write tests like this:

    "=Check Out" click()
    "#phone" sendKeys ("2222222222", Keys.ENTER)
    ".firstname".getValue should equal("TestPersonFirst")


Enjoy.

--
You received this message because you are subscribed to the Google Groups "Lift" group.
To post to this group, send email to liftweb-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org.
To unsubscribe from this group, send email to liftweb+unsubscribe <at> googlegroups.com.
For more options, visit this group at http://groups.google.com/group/liftweb?hl=en.

--
You received this message because you are subscribed to the Google Groups "Lift" group.
To post to this group, send email to liftweb-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To unsubscribe from this group, send email to liftweb+unsubscribe <at> googlegroups.com.
For more options, visit this group at http://groups.google.com/group/liftweb?hl=en.
Naftoli Gugenheim | 1 Jun 20:55 2011
Picon

Re: [Lift] Selenium 2 mini-DSL

Version 2:
Main enhancements are support for finding under an existing element: ".mydiv" / "select" and finding multiple (/+) or optional (/? -- returns a Box; if not found will be a Failure with the exception, since it uses tryo). These operators work on anything viewable as a SearchContext so you can use them on the whole document via the WebDriver.

Also uses scalatest's afterAll to quit the driver.



package moadim.kiosk

import org.scalatest.{BeforeAndAfterAll, Suite}

import org.openqa.selenium.{By, WebDriver, WebElement, SearchContext}
import org.openqa.selenium.support.ui.Select


import scala.collection.JavaConversions._

import net.liftweb.util.Helpers.tryo
import net.liftweb.common.Box


trait SeleniumDSL extends BeforeAndAfterAll {this: Suite =>
  
  val driver: WebDriver

  override def afterAll {
    driver.quit()
    super.afterAll
  }

  import driver._
  
  private def finder(c: Char): String => By = s => c match {
    case '#' => By id s
    case '.' => By className s
    case '$' => By cssSelector s
    case '%' => By xpath s
    case ':' => By name s
    case '=' => By linkText s
    case '~' => By partialLinkText s
    case _ => By tagName c + s
  }
  
  implicit def str2by(s: String): By = finder(s.charAt(0))(s.substring(1))
  
  implicit def by2El[T<%By](b: T): WebElement = driver / b
  
  implicit def el2Sel[T<%WebElement](el:T): Select = new Select(el)
  
  class Searchable(sc: SearchContext) {
    def /[T<%By](b: T): WebElement = sc.findElement(b)
    def /?[T<%By](b: T): Box[WebElement] = tryo(sc.findElement(b))
    def /+[T<%By](b: T): Seq[WebElement] = sc.findElements(b)
  }
  
  implicit def scDsl[T<%SearchContext](sc: T): Searchable = new Searchable(sc)
}



On Wed, Jun 1, 2011 at 11:37 AM, Paul Dale <paul.dale-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:
Very cool.

On Tue, May 31, 2011 at 8:52 AM, Naftoli Gugenheim <naftoligug-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:
To write tests for a web application, I seem to have ended up going with Selenium 2 via ScalaTest. I wrote a mini DSL:

  import driver._
  def finder(c: Char): String => WebElement = s => findElement(c match {
    case '#' => By id s
    case '.' => By className s
    case '$' => By cssSelector s
    case '%' => By xpath s
    case ':' => By name s
    case '=' => By linkText s
    case '~' => By partialLinkText s
    case _ => By tagName c + s
  })
  implicit def str2el(s: String): WebElement =
    s.toList match { case c :: cs => finder(c)(cs.mkString) }


So now I can write tests like this:

    "=Check Out" click()
    "#phone" sendKeys ("2222222222", Keys.ENTER)
    ".firstname".getValue should equal("TestPersonFirst")


Enjoy.

--
You received this message because you are subscribed to the Google Groups "Lift" group.
To post to this group, send email to liftweb-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org.
To unsubscribe from this group, send email to liftweb+unsubscribe <at> googlegroups.com.
For more options, visit this group at http://groups.google.com/group/liftweb?hl=en.

--
You received this message because you are subscribed to the Google Groups "Lift" group.
To post to this group, send email to liftweb-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org.
To unsubscribe from this group, send email to liftweb+unsubscribe <at> googlegroups.com.
For more options, visit this group at http://groups.google.com/group/liftweb?hl=en.

--
You received this message because you are subscribed to the Google Groups "Lift" group.
To post to this group, send email to liftweb-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To unsubscribe from this group, send email to liftweb+unsubscribe <at> googlegroups.com.
For more options, visit this group at http://groups.google.com/group/liftweb?hl=en.

Gmane