Kotlin 中的 Selenium => 操作不可用

Selenium in Kotlin => Actions not available

我正在尝试在 IntelliJ IDEA 中使用 Kotlin 通过 Selenium 上下文单击按钮。我需要 select table 的前 10 行,右键单击它们并单击上下文菜单中的条目。 因此我需要一个 Actions 对象——问题已经在这里开始了! "Actions" 写成红色,IntelliJ 拒绝导入 "org.openqa.selenium.interactions.Actions" 它说 "Unresolved reference: Actions"。但必须有办法做到这一点!我越来越绝望了,请帮帮我!

这是我的测试class:

import org.openqa.selenium.By
import org.openqa.selenium.WebDriver
import org.openqa.selenium.chrome.ChromeDriver
import org.testng.annotations.*
import java.awt.event.KeyEvent

class BatchProcessTest {

    init{
    System.setProperty("webdriver.chrome.driver", "C:\Users\<path to chromedriver>\chromedriver.exe")
}

private val driver = ChromeDriver() as WebDriver
private val testBase : TestBase = TestBase(driver)
private var isTestClassInitialized = false

@BeforeTest
private fun initiateTestSuite(){
    testBase.startTestObject()
    testBase.openWebPage()
    testBase.loginTestUserUsingCredentials(<username>,<userpassword>)
    testBase.waitForMilliseconds(5000)
    isTestClassInitialized = true
}

@Test
private fun testUnderConstruction() {

    while(!isTestClassInitialized){
        testBase.waitForMilliseconds(1000)
    }
    setPreconds()
}

private fun setPreconds(){

    prepareACategoryWithAtLeastTenEntriesAsK1()
}

private fun prepareACategoryWithAtLeastTenEntriesAsK1(){

    createAPattern(<patternName>, <patternRule>)
    executePatterns()
    testBase.getButtonByCaption("Back").click()
    changeIntoCategory(<name of category K0>)
    moveTenOfTheMovedEntriesIntoK1(<name of category K1>)


}

private fun createAPattern(text : String, category: String){

    val settingsButton = driver.findElements(By.className("lm-button")).get(0)
    settingsButton.click()
    testBase.waitForMilliseconds(3000)

    val addButton = testBase.getButtonByCaption("Add")
    addButton.click()

    val patternTextField = testBase.getTextFieldByLabelCaption("Pattern name")
    patternTextField.sendKeys(text)

    testBase.selectComboboxElementUsingLabelCaption(category,"Pattern name")
    testBase.waitForMilliseconds(1000)

    val saveButton = testBase.getButtonByCaption("Save")
    saveButton.click()
}

private fun executePatterns(){
    testBase.getButtonByCaption("Execute").click()

}

private fun changeIntoCategory(categoryName : String){
    testBase.getButtonByCaption(name of category K1).click()
    testBase.waitForMilliseconds(3000)
}

private fun moveTenOfTheMovedEntriesIntoK1(categoryName : String) {

val action = Actions(driver) 
val allLines = driver.findElements(By.className("v-grid-row"))
action.click(allLines.get(0)).perform()
var i = 0

while (i < 11) {
    i++
    action.click(allLines.get(i)).perform()
}

action.contextClick().perform()

}

这是我的 TestBase class(助手 class):

import org.openqa.selenium.By
import org.openqa.selenium.Keys
import org.openqa.selenium.WebDriver
import org.openqa.selenium.WebElement

class TestBase(driver: WebDriver) {

val driver: WebDriver = driver

fun startTestObject(): Boolean {

    val batchProcessBuilder = ProcessBuilder("cmd", "/c", "startTheTestObject.bat")
    batchProcessBuilder.start()
    return true
}

fun openWebPage() {
    System.setProperty("webdriver.chrome.driver", "src/test/kotlin/chromedriver.exe")
    driver.get(<URL of Web application>)
    driver.manage().window().maximize()
    waitForMilliseconds(5000)
}

fun loginTestUserUsingCredentials(usrName: String, usrPassword: String) {
    val userNameField = getTextFieldByLabelCaption("Username")
    val userPasswordField = getTextFieldByLabelCaption("Password")
    val loginButton = getButtonByCaption("Login")

    userNameField.sendKeys(usrName)
    userPasswordField.sendKeys(usrPassword)

    loginButton.click()
}

fun waitForMilliseconds(milliseconds: Long) {
    Thread.sleep(milliseconds)
}

fun getButtonByCaption(caption: String): WebElement {
    val allButtonsOnSettingsPage = driver.findElements(By.className("lm-button"))
    val filteredButtons = allButtonsOnSettingsPage.filter { it.text.contains(caption) }
    return filteredButtons.get(0)
}

fun getTextFieldByLabelCaption(caption: String): WebElement {

    val allFormLayoutsOnThisPage = driver.findElements(By.className("v-formlayout-row"))
    val filteredFormLayouts = allFormLayoutsOnThisPage.filter { it.findElement(By.className("v-formlayout-captioncell")).text.contains(caption) }
    val searchedTextField = filteredFormLayouts.get(0).findElement(By.className("v-textfield"))
    return searchedTextField
}

fun selectComboboxElementUsingLabelCaption(optionText: String, labelCaption: String) {
    val combobox = getComboboxdByLabelCaption(labelCaption)
    val window = driver.findElement(By.className("v-window"))

    combobox.click()
    waitForMilliseconds(2000)

    var i = 0
    var isEntryFound = false

    while (!isEntryFound){

        var actualElementText = driver.findElements(By.className("gwt-MenuItem")).get(i).text
        isEntryFound = actualElementText.equals(optionText)
        combobox.sendKeys(Keys.UP)
        waitForMilliseconds(700)
        i++
    }

    combobox.sendKeys(Keys.ENTER)
    waitForMilliseconds(1000)
}

fun getComboboxdByLabelCaption(caption: String): WebElement {

    waitForMilliseconds(2000)
    val allFormLayoutsOnThisPage = driver.findElements(By.className("v-formlayout-row"))
    val filteredFormLayouts = allFormLayoutsOnThisPage.filter { it.findElement(By.className("v-formlayout-captioncell")).text.contains(caption) }
    val searchedComboboxElement = filteredFormLayouts.get(0).findElement(By.className("v-filterselect-input"))
    return searchedComboboxElement
}
}

这是 POM:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>KotlinTest</groupId>
    <artifactId>KotlinTest</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>KotlinTest</name>
    <url>http://maven.apache.org</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <kotlin.version>1.1.2-4</kotlin.version>
        <testng.version>6.10</testng.version>
        <selenium.version>3.8.1</selenium.version>
        <selenium.server.standalone.version>3.8.1</selenium.server.standalone.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.jetbrains.kotlin</groupId>
            <artifactId>kotlin-stdlib</artifactId>
            <version>${kotlin.version}</version>
        </dependency>

        <dependency>
            <groupId>org.jetbrains.kotlin</groupId>
            <artifactId>kotlin-test-junit</artifactId>
            <version>${kotlin.version}</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>${selenium.version}</version>
        </dependency>

        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>${testng.version}</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-support</artifactId>
            <version>${selenium.version}</version>
        </dependency>

        <dependency>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.20.1</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>

         <dependency>
         <groupId>org.seleniumhq.selenium</groupId>
         <artifactId>selenium-server-standalone</artifactId>
         <version>${selenium-server-standalone.version}</version>
         </dependency>
    </dependencies>

    <build>
        <sourceDirectory>src/main/kotlin</sourceDirectory>
        <testSourceDirectory>src/test/kotlin</testSourceDirectory>

        <plugins>
            <plugin>
                <groupId>org.jetbrains.kotlin</groupId>
                <artifactId>kotlin-maven-plugin</artifactId>
                <version>${kotlin.version}</version>
                <executions>
                    <execution>
                        <id>compile</id>
                        <phase>compile</phase>
                        <goals>
                            <goal>compile</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>test-compile</id>
                        <phase>test-compile</phase>
                        <goals>
                            <goal>test-compile</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

非常感谢!

感谢您检查代码。拼写错误已删除,我找到了问题的原因。缺少存储库导致错误:

    <repositories> 
      <repository> 
         <id>jcenter</id> 
         <url>jcenter.bintray.com</url>
      </repository> 
    </repositories>

小心从 Internet 复制此内容 - 它可能会在 POM 中产生奇怪的效果。 (可能是字符损坏造成的)感谢您的帮助! – AcMuD 1 分钟前 编辑