Skip to content

Commit d24ee9f

Browse files
committed
Fix selection checks
1 parent a9864f0 commit d24ee9f

File tree

2 files changed

+44
-42
lines changed

2 files changed

+44
-42
lines changed

guide/selenium/src/test/scala/io/udash/web/SeleniumTest.scala

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
package io.udash.web
22

3+
import com.typesafe.scalalogging.StrictLogging
34
import io.github.bonigarcia.wdm.WebDriverManager
45
import org.openqa.selenium.firefox.{FirefoxDriver, FirefoxOptions}
56
import org.openqa.selenium.remote.RemoteWebDriver
67
import org.openqa.selenium.{By, Dimension, WebElement}
78
import org.scalatest.concurrent.Eventually
8-
import org.scalatest.time.{Millis, Seconds, Span}
9-
import org.scalatest.{BeforeAndAfterAll, BeforeAndAfterEach}
109
import org.scalatest.matchers.should.Matchers
10+
import org.scalatest.time.{Millis, Seconds, Span}
1111
import org.scalatest.wordspec.AnyWordSpec
12+
import org.scalatest.{BeforeAndAfterAll, BeforeAndAfterEach, Inspectors}
1213

1314
import java.time.Duration
1415

@@ -41,13 +42,15 @@ private final class InternalServerConfig extends ServerConfig {
4142

4243
override def createUrl(part: String): String = {
4344
require(part.startsWith("/"))
44-
s"http://127.0.0.2:${server.port}$part"
45+
s"http://localhost:${server.port}$part"
4546
}
4647
}
4748

48-
abstract class SeleniumTest extends AnyWordSpec with Matchers with BeforeAndAfterAll with BeforeAndAfterEach with Eventually {
49+
abstract class SeleniumTest extends AnyWordSpec
50+
with Matchers with BeforeAndAfterAll with BeforeAndAfterEach with Eventually with StrictLogging with Inspectors {
4951
override implicit val patienceConfig: PatienceConfig = PatienceConfig(scaled(Span(10, Seconds)), scaled(Span(50, Millis)))
5052

53+
logger.info("Setting up WebDriver")
5154
private val driverManager = WebDriverManager.firefoxdriver()
5255
driverManager.config().setServerPort(0)
5356
driverManager.setup()

guide/selenium/src/test/scala/io/udash/web/guide/demos/frontend/FrontendFormsTest.scala

Lines changed: 37 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package io.udash.web.guide.demos.frontend
22

3-
import com.avsystem.commons.*
43
import io.udash.web.SeleniumTest
54
import org.openqa.selenium.By.{ByClassName, ByCssSelector, ByTagName}
65

@@ -18,12 +17,12 @@ class FrontendFormsTest extends SeleniumTest {
1817
val checkbox = checkboxes.findElement(new ByClassName(s"checkbox-demo-$propertyName"))
1918
checkbox.click()
2019
eventually {
21-
checkboxes.findElements(new ByCssSelector(s"[data-bind=$propertyName]")).asScala.forall(el => {
22-
el.getText == expect
23-
}) should be(true)
24-
checkboxes.findElements(new ByClassName(s"checkbox-demo-$propertyName")).asScala.forall(el => {
25-
el.getDomAttribute("selected") == checkbox.getDomAttribute("selected")
26-
}) should be(true)
20+
forAll(checkboxes.findElements(new ByCssSelector(s"[data-bind=$propertyName]")))(el =>
21+
el.getText shouldBe expect
22+
)
23+
forAll(checkboxes.findElements(new ByClassName(s"checkbox-demo-$propertyName")))(el =>
24+
el.isSelected shouldBe checkbox.isSelected
25+
)
2726
}
2827
}
2928

@@ -44,13 +43,13 @@ class FrontendFormsTest extends SeleniumTest {
4443
val checkbox = checkButtons.findElement(new ByCssSelector(s"[data-label=$propertyName]")).findElement(new ByTagName("input"))
4544
checkbox.click()
4645
eventually {
47-
checkButtons.findElements(new ByClassName("check-buttons-demo-fruits")).asScala.forall(el => {
46+
forAll(checkButtons.findElements(new ByClassName("check-buttons-demo-fruits")))(el => {
4847
val contains = el.getText.contains(propertyName)
49-
if (checkbox.getDomAttribute("selected") != null) contains else !contains
50-
}) should be(true)
51-
checkButtons.findElements(new ByCssSelector(s"[data-label=$propertyName]")).asScala.forall(el => {
52-
el.findElement(new ByTagName("input")).getDomAttribute("selected") == checkbox.getDomAttribute("selected")
53-
}) should be(true)
48+
assert(if (checkbox.isSelected) contains else !contains)
49+
})
50+
forAll(checkButtons.findElements(new ByCssSelector(s"[data-label=$propertyName]")))(el =>
51+
el.findElement(new ByTagName("input")).isSelected shouldBe checkbox.isSelected
52+
)
5453
}
5554
}
5655

@@ -67,13 +66,13 @@ class FrontendFormsTest extends SeleniumTest {
6766
val option = select.findElement(new ByCssSelector(s"[value='$propertyIdx']"))
6867
option.click()
6968
eventually {
70-
multiSelect.findElements(new ByClassName("multi-select-demo-fruits")).asScala.forall(el => {
69+
forAll(multiSelect.findElements(new ByClassName("multi-select-demo-fruits")))(el => {
7170
val contains = el.getText.contains(propertyName)
72-
if (option.getDomAttribute("selected") != null) contains else !contains
73-
}) should be(true)
74-
multiSelect.findElements(new ByTagName("select")).asScala.forall(el => {
75-
el.findElement(new ByCssSelector(s"[value='$propertyIdx']")).getDomAttribute("selected") == option.getDomAttribute("selected")
76-
}) should be(true)
71+
assert(if (option.isSelected) contains else !contains)
72+
})
73+
forAll(multiSelect.findElements(new ByTagName("select")))(el => {
74+
el.findElement(new ByCssSelector(s"[value='$propertyIdx']")).isSelected shouldBe option.isSelected
75+
})
7776
}
7877
}
7978

@@ -91,13 +90,13 @@ class FrontendFormsTest extends SeleniumTest {
9190
val radio = radioButtons.findElement(new ByCssSelector(s"[data-label=$propertyName]")).findElement(new ByTagName("input"))
9291
driver.executeScript("arguments[0].click();", radio)
9392
eventually {
94-
radioButtons.findElements(new ByClassName("radio-buttons-demo-fruits")).asScala.forall(el => {
95-
el.getText == propertyName
96-
}) should be(true)
97-
radioButtons.findElements(new ByCssSelector(s"input")).asScala.forall(el => {
98-
val eq = el.getDomAttribute("selected") == radio.getDomAttribute("selected")
99-
if (el.getDomProperty("value").toInt == propertyIdx) eq else !eq
100-
}) should be(true)
93+
forAll(radioButtons.findElements(new ByClassName("radio-buttons-demo-fruits")))(el =>
94+
el.getText shouldBe propertyName
95+
)
96+
forAll(radioButtons.findElements(new ByCssSelector(s"input")))(el => {
97+
val eq = el.isSelected == radio.isSelected
98+
assert(if (el.getDomProperty("value").toInt == propertyIdx) eq else !eq)
99+
})
101100
}
102101
}
103102

@@ -116,12 +115,12 @@ class FrontendFormsTest extends SeleniumTest {
116115
val option = select.findElement(new ByCssSelector(s"[value='$propertyIdx']"))
117116
option.click()
118117
eventually {
119-
selectDemo.findElements(new ByClassName("select-demo-fruits")).asScala.forall(el => {
120-
el.getText == propertyName
121-
}) should be(true)
122-
selectDemo.findElements(new ByTagName(s"select")).asScala.forall(el => {
123-
el.findElement(new ByCssSelector(s"[value='$propertyIdx']")).getDomAttribute("selected") == option.getDomAttribute("selected")
124-
}) should be(true)
118+
forAll(selectDemo.findElements(new ByClassName("select-demo-fruits")))(el => {
119+
el.getText shouldBe propertyName
120+
})
121+
forAll(selectDemo.findElements(new ByTagName(s"select")))(el => {
122+
el.findElement(new ByCssSelector(s"[value='$propertyIdx']")).isSelected shouldBe option.isSelected
123+
})
125124
}
126125
}
127126

@@ -140,9 +139,9 @@ class FrontendFormsTest extends SeleniumTest {
140139
textArea.clear()
141140
textArea.sendKeys(text)
142141
eventually {
143-
textAreaDemo.findElements(new ByTagName(s"textarea")).asScala.forall(el => {
144-
el.getDomProperty("value") == text
145-
}) should be(true)
142+
forAll(textAreaDemo.findElements(new ByTagName(s"textarea")))(el => {
143+
el.getDomProperty("value") shouldBe text
144+
})
146145
}
147146
}
148147

@@ -159,9 +158,9 @@ class FrontendFormsTest extends SeleniumTest {
159158
input.clear()
160159
input.sendKeys(text)
161160
eventually {
162-
inputsDemo.findElements(new ByCssSelector(s"input[type=$tpe]")).asScala.forall(el => {
163-
el.getDomProperty("value") == text
164-
}) should be(true)
161+
forAll(inputsDemo.findElements(new ByCssSelector(s"input[type=$tpe]")))(el => {
162+
el.getDomProperty("value") shouldBe text
163+
})
165164
}
166165
}
167166

0 commit comments

Comments
 (0)