美文网首页
Selenide && 应用案例

Selenide && 应用案例

作者: 嗚嗚雲 | 来源:发表于2022-04-20 11:34 被阅读0次

    利用selenide 面去繁琐的配置,直接上代码

            <dependency>
              <groupId>com.codeborne</groupId>
                <artifactId>selenide</artifactId>
                <version>5.21.0</version>
            </dependency>
    

    以下是一个操作实例的部分代码:

    public class TestServiceImpl implements TestService {
    
        @Autowired
        ConfigProps configProps;
    
        @Override
        public void read() {
    
            // 默认报告目录
            Configuration.reportsFolder = "test-result/reports";
            // 设置chromedriver位置
            // System.setProperty("webdriver.chrome.driver", wanFangProps.getChromeDriverPath());
    
            System.setProperty(ChromeDriverService.CHROME_DRIVER_SILENT_OUTPUT_PROPERTY, "true");
            DesiredCapabilities desiredCapabilities = DesiredCapabilities.chrome();
            desiredCapabilities.setBrowserName(Browsers.CHROME);
    
    
            ChromeOptions chromeOptions = new ChromeOptions();
            chromeOptions.addArguments();
    
            // 设置下载路径
            HashMap<String, Object> chromePrefs = new HashMap<String, Object>();
            chromePrefs.put("download.default_directory", configProps.getDownloadsPath());
            chromePrefs.put("download.prompt_for_download", false);
            chromePrefs.put("disable-popup-blocking", true);
            chromeOptions.setExperimentalOption("prefs", chromePrefs);
            
            Configuration.browserCapabilities = chromeOptions;
            WebDriverRunner.clearBrowserCache();
    
            // Setting Browser type instead Firefox by default
            Configuration.browser = "chrome";
            Configuration.timeout = 80000;
    
             wanfang();
        }
    
        private void wanfang() {
            open("http://med.wanfangdata.com.cn/");
            WebDriverRunner.getWebDriver().manage().window().maximize();
            $(By.id("loginUrl")).click();
            $(By.id("UserName")).setValue(configProps.getUser());
            $(By.id("Password")).setValue(configProps.getPass());
    
            String message = "请在确认后输入验证码,等待" + configProps.getSecond() + "秒";
            executeJavaScript("alert('" + message + "')");
            sleep(configProps.getSecond() * 1000);
            $(By.className("btn-login")).click();
    
            $(By.className("input_search0")).setValue(configProps.getSearch());
            $(By.className("MedSearchBtn")).click();
    
            int i = 0;
            while (i < configProps.getPages()) {
                $$(".btn-download").iterator().forEachRemaining(x -> {
                    log.info(x.getAttribute("href"));
                    open(x.getAttribute("href"));
                    sleep(1000);
                });
                $(By.className("clicknext")).click();
                i++;
            }
        }
    

    了解更多,欢迎私信

    相关文章

      网友评论

          本文标题:Selenide && 应用案例

          本文链接:https://www.haomeiwen.com/subject/jkcvertx.html