package teststate.selenium

import org.openqa.selenium._

object SeleniumExt {

  class WebElementExt(private val self: WebElement) extends AnyVal {
    def clearAndSendKeys(keys: String): Unit = {
      self.clear()
      self.sendKeys(keys)
    }

    def scrollTo(d: WebDriver): Unit = {
      val p = self.getLocation
      _scrollTo(d, Some(p.x), Some(p.y))
    }

    def scrollToX(d: WebDriver): Unit =
      _scrollTo(d, Some(self.getLocation.x), None)

    def scrollToY(d: WebDriver): Unit =
      _scrollTo(d, None, Some(self.getLocation.y))

    private def _scrollTo(d: WebDriver, x: Option[Int], y: Option[Int]): Unit =
      d match {
        case j: JavascriptExecutor =>
          val xx = x.fold("window.scrollX")(_.toString)
          val yy = y.fold("window.scrollY")(_.toString)
          j.executeScript(s"window.scrollTo($xx,$yy)")
          ()
        case _ =>
          throw new JavaScriptNotSupported("Scrolling without JS not supported.")
      }

    def scrollToAndClick(d: WebDriver): Unit = {
      scrollTo(d)
      self.click()
    }

  }

  class PointExt(private val p: Point) extends AnyVal {
    def isPositive: Boolean =
      p.x >= 0 && p.y >= 0
  }

  final class JavaScriptNotSupported(msg: String) extends RuntimeException
}

trait SeleniumExt {
  import SeleniumExt._
  implicit def WebElementExt(e: WebElement): WebElementExt = new WebElementExt(e)
  implicit def PointExt     (p: Point)     : PointExt      = new PointExt(p)
}
package teststate.selenium

import org.openqa.selenium.{Point, WebElement}

object SeleniumExt {

  class WebElementExt(private val e: WebElement) extends AnyVal {
    def clearAndSendKeys(keys: String): Unit = {
      e.clear()
      e.sendKeys(keys)
    }
  }

  class PointExt(private val p: Point) extends AnyVal {
    def isPositive: Boolean =
      p.x >= 0 && p.y >= 0
  }

}

trait SeleniumExt {
  import SeleniumExt._
  implicit def WebElementExt(e: WebElement): WebElementExt = new WebElementExt(e)
  implicit def PointExt     (p: Point)     : PointExt      = new PointExt(p)
}
--- 
+++ 
@@ -1,14 +1,42 @@
 package teststate.selenium
 
-import org.openqa.selenium.{Point, WebElement}
+import org.openqa.selenium._
 
 object SeleniumExt {
 
-  class WebElementExt(private val e: WebElement) extends AnyVal {
+  class WebElementExt(private val self: WebElement) extends AnyVal {
     def clearAndSendKeys(keys: String): Unit = {
-      e.clear()
-      e.sendKeys(keys)
+      self.clear()
+      self.sendKeys(keys)
     }
+
+    def scrollTo(d: WebDriver): Unit = {
+      val p = self.getLocation
+      _scrollTo(d, Some(p.x), Some(p.y))
+    }
+
+    def scrollToX(d: WebDriver): Unit =
+      _scrollTo(d, Some(self.getLocation.x), None)
+
+    def scrollToY(d: WebDriver): Unit =
+      _scrollTo(d, None, Some(self.getLocation.y))
+
+    private def _scrollTo(d: WebDriver, x: Option[Int], y: Option[Int]): Unit =
+      d match {
+        case j: JavascriptExecutor =>
+          val xx = x.fold("window.scrollX")(_.toString)
+          val yy = y.fold("window.scrollY")(_.toString)
+          j.executeScript(s"window.scrollTo($xx,$yy)")
+          ()
+        case _ =>
+          throw new JavaScriptNotSupported("Scrolling without JS not supported.")
+      }
+
+    def scrollToAndClick(d: WebDriver): Unit = {
+      scrollTo(d)
+      self.click()
+    }
+
   }
 
   class PointExt(private val p: Point) extends AnyVal {
@@ -16,6 +44,7 @@
       p.x >= 0 && p.y >= 0
   }
 
+  final class JavaScriptNotSupported(msg: String) extends RuntimeException
 }
 
 trait SeleniumExt {
Add Selenium ext to scroll to element
