Kotlin中的硒=>行动不可用

时间:2018-01-18 08:31:15

标签: selenium intellij-idea kotlin action

我正在尝试使用IntelliJ IDEA中的Kotlin单击Selenium按钮。我需要选择表格的前10行,右键单击它们并单击上下文菜单中的条目。 因此我需要一个Actions对象 - 问题就在这里! “动作”是用红色写的,IntelliJ拒绝导入“org.openqa.selenium.interactions.Actions”它说“未解决的参考:动作”。但必须有办法做到这一点!我越来越绝望了,请帮助我!

这是我的测试类:

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类(帮助类):

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>

非常感谢!

1 个答案:

答案 0 :(得分:0)

感谢您检查代码。删除错别字,我找到了问题的原因。丢失的存储库导致错误:

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

小心地从互联网上复制它 - 它可以在POM中产生奇怪的效果。 (可能由破坏的字符引起)感谢您的帮助! - AcMuD 1分钟前编辑