Julio
Julio

Reputation: 3

Selenium and scalaTest error

i have a problem,

I'm trying to follow a chapter 5 of the book "sbt in Action" And i have a problem that i don't understand what is wrong...

this is my SeleniumSpec.scala

package org.preownedkittens;

import org.scalatest._
import org.scalatest.matchers.ShouldMatchers
import org.scalatest.events._
import org.scalatest.selenium._
import org.openqa.selenium.WebDriver


import org.scalatest._
import org.scalatest.matchers.ShouldMatchers
import org.scalatest.events._
import org.scalatest.selenium._
import org.scalatest.junit._
import org.openqa.selenium.WebDriver

class SeleniumSpec extends FlatSpec with ShouldMatchers with BeforeAndAfter with BeforeAndAfterAll with Chrome {
  val homePage: String = "http://localhost:9000"

  "Home page" should "redirect to kitten list" in {
    go to "http://localhost:9000"
    currentUrl should startWith ("http://localhost:9000/kittens")
  }

  it should "show three dropdown lists of attributes in sorted order" in {
    def select(name: String) = findAll(xpath("//select[@name='" + name + "']/option")).map { _.text }.toList
    def assertListCompleteAndIsSorted(list: Seq[String]) = {
      list.size should be(20)
      list.sorted should be(list)
    }

    go to homePage + "/kittens"

    assertListCompleteAndIsSorted(select("select1"))
    assertListCompleteAndIsSorted(select("select2"))
    assertListCompleteAndIsSorted(select("select3"))
  }

  private def purchaseForms() = findAll(xpath("//form/li/input[@id='purchase']/..")).map { _.text }.toList

  override def afterAll() {
    webDriver.quit()
  }
}

and when i execute the test, i have this error:

[info] SeleniumSpec:
[info] Home page
[info] - should redirect to kitten list *** FAILED ***
[info]   "http://localhost:9000/" did not start with substring "http://localhost:9000/kittens" (SeleniumSpec.scala:22)
[info] - should show three dropdown lists of attributes in sorted order *** FAILED ***
[info]   0 was not equal to 20 (SeleniumSpec.scala:28)
[info] Run completed in 4 seconds, 585 milliseconds.
[info] Total number of tests run: 2
[info] Suites: completed 1, aborted 0
[info] Tests: succeeded 0, failed 2, canceled 0, ignored 0, pending 0
[info] *** 2 TESTS FAILED ***
[error] Failed tests:
[error]     org.preownedkittens.SeleniumSpec
[error] (website/it:test) sbt.TestsFailedException: Tests unsuccessful
[error] Total time: 19 s, completed 20-feb-2016 11:51:48

I have the chrome drive updated, (chrome app start but after second it close and the error appears)

Upvotes: 0

Views: 692

Answers (2)

nexoma
nexoma

Reputation: 285

you have a not started webdriver:

          System.setProperty("webdriver.chrome.driver", "bin\\chromedriver.exe")

    val dc = DesiredCapabilities.chrome()
    wd = new ChromeDriver(dc) // manage chrome through chromedriver
    //    wd.get(host)  or go to host
    //    wd.manage().timeouts().implicitlyWait(50, TimeUnit.SECONDS)
    //    wd.manage().timeouts().implicitlyWait(0, TimeUnit.SECONDS)

Upvotes: 0

Andrew Regan
Andrew Regan

Reputation: 5113

Firstly, your "Home Page" test goes to http://localhost:9000, but without any redirect (there is one in your next test) your currentUrl should startWith check correctly fails because of the URL mismatch.

Your "show three dropdown lists" test is probably failing because you're not waiting - after the go to homePage + "/kittens" request - for the page to fully load before (milliseconds later) your XPath queries run and return the number of matching elements. Zero is a quite plausible result if the page is only partially loaded.

Probably the best solution is to identify another element that should appear on the page in question (perhaps a footer?) and wait for it to appear:

WebDriverWait wait = new WebDriverWait(driver, 2000);  // 2 secs
wait.until(ExpectedConditions.presenceOfElementLocated(locator)); 

At which point you can assume the page is fully loaded, and check the actual dropdown counts.

Upvotes: 1

Related Questions