Skip to content

Commit 8820d4a

Browse files
committed
Added infix modifier to WebBrowser infix functions for Scala 3 build.
1 parent 01b0803 commit 8820d4a

File tree

3 files changed

+70
-11
lines changed

3 files changed

+70
-11
lines changed

build.sbt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,12 @@ scalacOptions ++= {
125125
) ++ (if (scalaBinaryVersion.value == "2.10" || scalaBinaryVersion.value.startsWith("3.")) Seq.empty else Seq("-Ywarn-unused"))
126126
}
127127

128+
Compile / sourceGenerators += {
129+
Def.task {
130+
Generator.generateWebBrowserForScala3(scalaVersion.value, (sourceManaged in Compile).value)
131+
}.taskValue
132+
}
133+
128134
// Temporary disable publishing of doc in dotty, can't get it to build.
129135
Compile / packageDoc / publishArtifact := !scalaBinaryVersion.value.startsWith("3")
130136

project/Generator.scala

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* Copyright 2001-2022 Artima, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
import sbt.IO
18+
19+
import scala.io.Source
20+
import java.io.{File, FileWriter, BufferedWriter}
21+
22+
object Generator {
23+
24+
private def copyFile(sourceFile: File, destFile: File): File = {
25+
val destWriter = new BufferedWriter(new FileWriter(destFile))
26+
try {
27+
val lines = Source.fromFile(sourceFile).getLines.toList
28+
var skipMode = false
29+
for (line <- lines) {
30+
destWriter.write(line.replace("/*infix*/", "infix"))
31+
destWriter.newLine()
32+
}
33+
destFile
34+
}
35+
finally {
36+
destWriter.flush()
37+
destWriter.close()
38+
println("Copied " + destFile.getAbsolutePath)
39+
}
40+
}
41+
42+
def generateWebBrowserForScala3(scalaV: String, targetDir: File): Seq[File] = {
43+
if (scalaV.startsWith("3.")) {
44+
targetDir.mkdirs()
45+
val sourceFile = new File("src/main/scala-2/org/scalatestplus/selenium/WebBrowser.scala")
46+
val targetFile = new File(targetDir, "WebBrowser.scala")
47+
List(copyFile(sourceFile, targetFile))
48+
}
49+
else
50+
List.empty
51+
}
52+
53+
}

src/main/scala/org/scalatestplus/selenium/WebBrowser.scala renamed to src/main/scala-2/org/scalatestplus/selenium/WebBrowser.scala

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2761,7 +2761,7 @@ trait WebBrowser {
27612761
* @param url the URL to which to send the browser
27622762
* @param driver the <code>WebDriver</code> with which to drive the browser
27632763
*/
2764-
def to(url: String)(implicit driver: WebDriver): Unit = {
2764+
/*infix*/ def to(url: String)(implicit driver: WebDriver): Unit = {
27652765
driver.get(url)
27662766
}
27672767

@@ -2780,7 +2780,7 @@ trait WebBrowser {
27802780
* @param page the <code>Page</code> object containing the URL to which to send the browser
27812781
* @param driver the <code>WebDriver</code> with which to drive the browser
27822782
*/
2783-
def to(page: Page)(implicit driver: WebDriver): Unit = {
2783+
/*infix*/ def to(page: Page)(implicit driver: WebDriver): Unit = {
27842784
driver.get(page.url)
27852785
}
27862786
}
@@ -3889,7 +3889,7 @@ trait WebBrowser {
38893889
*
38903890
* @param element the <code>WebElement</code> to click on
38913891
*/
3892-
def on(element: WebElement): Unit = {
3892+
/*infix*/ def on(element: WebElement): Unit = {
38933893
element.click()
38943894
}
38953895

@@ -3899,7 +3899,7 @@ trait WebBrowser {
38993899
* @param query the <code>Query</code> with which to search
39003900
* @param driver the <code>WebDriver</code> with which to drive the browser
39013901
*/
3902-
def on(query: Query)(implicit driver: WebDriver): Unit = {
3902+
/*infix*/ def on(query: Query)(implicit driver: WebDriver): Unit = {
39033903
query.webElement.click()
39043904
}
39053905

@@ -3909,7 +3909,7 @@ trait WebBrowser {
39093909
* @param queryString the string with which to search, first by ID then by name
39103910
* @param driver the <code>WebDriver</code> with which to drive the browser
39113911
*/
3912-
def on(queryString: String)(implicit driver: WebDriver, pos: source.Position = implicitly[source.Position]): Unit = {
3912+
/*infix*/ def on(queryString: String)(implicit driver: WebDriver, pos: source.Position = implicitly[source.Position]): Unit = {
39133913
// stack depth is not correct if just call the button("...") directly.
39143914
val target = tryQueries(queryString)(q => q.webElement)
39153915
on(target)
@@ -3920,7 +3920,7 @@ trait WebBrowser {
39203920
*
39213921
* @param element the <code>Element</code> to click on
39223922
*/
3923-
def on(element: Element): Unit = {
3923+
/*infix*/ def on(element: Element): Unit = {
39243924
element.underlying.click()
39253925
}
39263926
}
@@ -4062,7 +4062,7 @@ trait WebBrowser {
40624062
* @param driver the <code>WebDriver</code> with which to drive the browser
40634063
* @return instance of specified <code>SwitchTarget</code>'s type parameter
40644064
*/
4065-
def to[T](target: SwitchTarget[T])(implicit driver: WebDriver, pos: source.Position = implicitly[source.Position]): T = {
4065+
/*infix*/ def to[T](target: SwitchTarget[T])(implicit driver: WebDriver, pos: source.Position = implicitly[source.Position]): T = {
40664066
target.switch(driver)(pos)
40674067
}
40684068
}
@@ -4321,7 +4321,7 @@ trait WebBrowser {
43214321
* @param name cookie's name
43224322
* @param driver the <code>WebDriver</code> with which to drive the browser
43234323
*/
4324-
def cookie(name: String)(implicit driver: WebDriver, pos: source.Position = implicitly[source.Position]): Unit = {
4324+
/*infix*/ def cookie(name: String)(implicit driver: WebDriver, pos: source.Position = implicitly[source.Position]): Unit = {
43254325
deleteCookie(name)
43264326
}
43274327

@@ -4330,7 +4330,7 @@ trait WebBrowser {
43304330
*
43314331
* @param driver the <code>WebDriver</code> with which to drive the browser
43324332
*/
4333-
def all(cookies: CookiesNoun)(implicit driver: WebDriver, pos: source.Position = implicitly[source.Position]): Unit = {
4333+
/*infix*/ def all(cookies: CookiesNoun)(implicit driver: WebDriver, pos: source.Position = implicitly[source.Position]): Unit = {
43344334
driver.manage.deleteAllCookies()
43354335
}
43364336
}
@@ -4417,7 +4417,7 @@ trait WebBrowser {
44174417
*
44184418
* @param fileName screenshot file name, if does not end with .png, it will be extended automatically
44194419
*/
4420-
def to(fileName: String)(implicit driver: WebDriver): Unit = {
4420+
/*infix*/ def to(fileName: String)(implicit driver: WebDriver): Unit = {
44214421
driver match {
44224422
case takesScreenshot: TakesScreenshot =>
44234423
val tmpFile = takesScreenshot.getScreenshotAs(OutputType.FILE)
@@ -4433,7 +4433,7 @@ trait WebBrowser {
44334433
*
44344434
* @param dirName directory name to save screenshot.
44354435
*/
4436-
def toDir(dirName: String)(implicit driver: WebDriver): Unit = {
4436+
/*infix*/ def toDir(dirName: String)(implicit driver: WebDriver): Unit = {
44374437
driver match {
44384438
case takesScreenshot: TakesScreenshot =>
44394439
val tmpFile = takesScreenshot.getScreenshotAs(OutputType.FILE)

0 commit comments

Comments
 (0)