我是scala的新手。以下与我的问题相关的4个类存在于Scala中设计的测试自动化框架中。是否可以访问对象(LoginPage)的成员(日,月,年),如果它作为其超类(PageObject)的实例传递给方法,并且超类不包含这些成员
trait PageObject {
val rootUrl = "http://localhost:9999/basic-page"
val back = "backLink"
val submit = "submit"
val url: String
val heading: String
}
object LoginPage extends PageObject {
val url =s"$rootUrl/login-page"
val heading = "Heading heading"
val day = "login_page_day"
val month = "login_page__month"
val year = "login_page__year"
}
import LoginPage
object Main {
def main(args: Array[String]) {
val date = "15 August 2017"
enterDateOnPage(date, LoginPage)
}
}
<current way>
class DateMethods {
def enterDateOnPage(date: String, page: PageObject) {
val arr = date.split(" ")
val cal = Calendar.getInstance()
cal.setTime(new SimpleDateFormat("MMMM").parse(arr(1)))
numberField(LoginPage.day).value = arr(0)
numberField(LoginPage.month).value = (cal.get(Calendar.MONTH) + 1).toString
numberField(LoginPage.year).value = arr(2)
click on name(LoginPage.submit)
}
}
<preferred way>
class DateMethods {
def enterDateOnPage(date: String, page: PageObject) {
val arr = date.split(" ")
val cal = Calendar.getInstance()
cal.setTime(new SimpleDateFormat("MMMM").parse(arr(1)))
numberField(page.day).value = arr(0)
numberField(page.month).value = (cal.get(Calendar.MONTH) + 1).toString
numberField(page.year).value = arr(2)
click on name(page.submit)
}
}
&#39; LoginPage.day&#39;而这些喜欢被&#39; page.day&#39;取代确认pageMatch实际上是否返回了LoginPage?
我确实根据Samuel Heaney的回答尝试了这样的事情,但它不会编译
def enterDateOnPage(date: String, page: PageObject) {
val npage = getPage(page)
val arr = date.split(" ")
val cal = Calendar.getInstance()
cal.setTime(new SimpleDateFormat("MMMM").parse(arr(1)))
numberField(npage.day).value = arr(0)
numberField(npage.month).value = (cal.get(Calendar.MONTH) + 1).toString
numberField(npage.year).value = arr(2)
click on name(npage.submit)
}
def getPage(page:PageObject)={
page match{
case _:LoginPage.type => LoginPage
case _:EntryFormPage.type => EntryFormPage
}
只有在我添加日,月和年作为PageObject特征的成员时才有效,但这对我来说不是一个可行的选择
答案 0 :(得分:1)
您可以使用模式匹配来测试它是否为LoginPage
。
class DateMethods {
def enterDateOnPage(date: String, page: PageObject): Unit = {
page match {
case LoginPage =>
setUIFields(
LoginPage.day,
LoginPage.month,
LoginPage.year,
date
)
case LogoutPage =>
setUIFields(...)
case EntryFormPage =>
setUIFields(...)
}
click on name(page.submit)
}
def setUIFields(
day: String,
month: String,
year: String,
date: String
): Unit = {
val arr = date.split(" ")
val cal = Calendar.getInstance()
cal.setTime(new SimpleDateFormat("MMMM").parse(arr(1)))
numberField(day).value = arr(0)
numberField(month).value = (cal.get(Calendar.MONTH) + 1).toString
numberField(year).value = arr(2)
}
}
答案 1 :(得分:0)
将日期逻辑拉入自己的特征并与之匹配。
trait Dates {
val day: String
val month: String
val year: String
}
case object LoginPage extends PageObject with Dates {
val day = ""
val month = ""
val year = ""
// additional code
}
现在你的功能可以特别匹配
def enterDateOnPage(date: String, page: PageObject) {
page match {
case datePage: Dates => {
// add log to validate date fields
}
case _ => // no-op
}
// additional code
}