代码之家  ›  专栏  ›  技术社区  ›  Alex

在宁静电影剧本中上传文件的正确方法

  •  0
  • Alex  · 技术社区  · 6 年前

    目前,通过maven使用最新的serenity版本(2.0.2),我可以简单地执行如下上传操作:

    import net.serenitybdd.core.annotations.findby.By;
    import net.serenitybdd.screenplay.Actor;
    import net.serenitybdd.screenplay.Task;
    import net.serenitybdd.screenplay.actions.Upload;
    import net.serenitybdd.screenplay.targets.Target;
    import net.serenitybdd.screenplay.waits.WaitUntil;
    
    import java.nio.file.Path;
    import java.nio.file.Paths;
    
    import static net.serenitybdd.screenplay.Tasks.instrumented;
    import static net.serenitybdd.screenplay.matchers.WebElementStateMatchers.isVisible;
    
    public class PerformTheUpload implements Task {
    
        public static Target UPLOAD_SOURCE_FILES_TARGET =
                Target.the("Upload source files").located(By.id("uploadSourceFiles"));
        public static final String TEST_FILE = "src/main/resources/files/test.txt";
        public static final Path TEST_FILE_PATH = Paths.get(TEST_FILE).toAbsolutePath();
    
        public static PerformTheUpload onTheField() {
            return instrumented(PerformTheUpload.class);
        }
    
        @Override public <T extends Actor> void performAs(T actor) {        
            actor.attemptsTo(WaitUntil.the(UPLOAD_SOURCE_FILES_TARGET, isVisible()));
            actor.attemptsTo(Upload.theFile(TEST_FILE_PATH).to(UPLOAD_SOURCE_FILES_TARGET));
        }
    }
    

    org.openqa.selenium.UnsupportedCommandException: POST /session/76ec390f-dbbe-48c2-be32-931969d81210/file did not match a known command
    Build info: version: '3.14.0', revision: 'aacccce0', time: '2018-08-02T20:19:58.91Z'
    System info: host: 'TAG-614', ip: '10.10.37.89', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '1.8.0_181'
    Driver info: org.openqa.selenium.remote.RemoteWebDriver
    Capabilities {acceptInsecureCerts: true, browserName: firefox, browserVersion: 62.0.3, javascriptEnabled: true, moz:accessibilityChecks: false, moz:geckodriverVersion: 0.22.0, moz:headless: false, moz:processID: 795608, moz:profile: C:\Users\alexandru.arcan\Ap..., moz:useNonSpecCompliantPointerOrigin: false, moz:webdriverClick: true, pageLoadStrategy: normal, platform: XP, platformName: XP, platformVersion: 10.0, rotatable: false, timeouts: {implicit: 0, pageLoad: 300000, script: 30000}}
    Session ID: 76ec390f-dbbe-48c2-be32-931969d81210
    Build info: version: '3.14.0', revision: 'aacccce0', time: '2018-08-02T20:19:58.91Z'
    System info: host: 'TAG-614', ip: '10.10.37.89', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '1.8.0_181'
    Driver info: driver.version: unknown
    

    有人能给我指一下正确的方向吗?

    1 回复  |  直到 6 年前
        1
  •  0
  •   Alex    6 年前

    通过实现myUploadFile方法找到了解决方案:

    ...
    import net.thucydides.core.pages.components.FileToUpload;
    
    public class PerformTheUpload  implements Task {
    
        private WebDriver driver;
    
        public PerformTheUpload (WebDriver driver){
            this.driver = driver;
        }
    
        public static PerformTheUpload onTheField(WebDriver driver) {
            return instrumented(PerformTheUpload.class, driver);
        }
    
    @Override public <T extends Actor> void performAs(T actor) {        
            actor.attemptsTo(WaitUntil.the(UPLOAD_SOURCE_FILES_TARGET, isVisible()));
            myUploadFile(driver);
        }
    
        public void myUploadFile(WebDriver driver){
            WebElement webElement = getUploadWebElementById("uploadSourceFiles", driver);
            FileToUpload fileToUpload = new FileToUpload(driver, TEST_FILE);
            fileToUpload.fromLocalMachine().to(webElement);
        }
    
    }
    
        //in the page class
        public static WebElement getUploadWebElementById(String id, WebDriver driver) {
            return driver.findElement(By.id(id));
        }