自动化测试(Java+Selenium)
import org.omg.PortableInterceptor.ACTIVE;
import org.openqa.selenium.By;
import org.openqa.selenium.Dimension;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.Assert;
import org.testng.asserts.Assertion;


import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;

public class Test {
    public static void main(String[] args) throws InterruptedException {
        // 设置浏览器驱动
        System.setProperty("webdriver.gecko.driver", "geckodriver.exe");
        // 初始化浏览器驱动
        WebDriver driver = new FirefoxDriver();
        // 设置网址
        String url = "https://www.baidu.com";
        // 打开网址
        driver.get(url);

        // 元素定位的6种方法
        WebElement id = driver.findElement(By.id(""));
        WebElement name = driver.findElement(By.name(""));
        WebElement class_name = driver.findElement(By.className(""));
        WebElement tag_name = driver.findElement(By.tagName(""));
        WebElement link_text = driver.findElement(By.linkText(""));
        WebElement partial_link_text = driver.findElement(By.partialLinkText(""));
        WebElement xpath = driver.findElement(By.xpath(""));
        List<WebElement> css = driver.findElements(By.cssSelector(""));

        // 最大化浏览器窗口
        driver.manage().window().maximize();
        // 窗口全屏
        driver.manage().window().fullscreen();
        // 窗口固定大小
        driver.manage().window().setSize(new Dimension(666,555));
        // 获取当前窗口句柄
        String windows_id = driver.getWindowHandle();
        // 获取所有窗口句柄
        Set<String> windows_ids = driver.getWindowHandles();
        // 切换对应id的窗口
        driver.switchTo().window(windows_id);

        // 实例化鼠标对象
        Actions actions = new Actions(driver);
        // 触发鼠标操作
        actions.perform();
        // 模拟用户按下鼠标左键并保持不放
        actions.clickAndHold();
        // 将鼠标移动到元素的中心
        actions.moveToElement(id);
        // 将鼠标从其当前位置(或 0, 0)偏移给定的偏移量。
        // xOffset-设置水平偏移(负值-向左移动鼠标), y-Offset-设置垂直偏移(负值-向上移动鼠标)
        actions.moveByOffset(0, 0);
        // 释放鼠标之前,单击源位置并移动到目标元素的位置
        actions.dragAndDrop(id, tag_name);
        // 在源位置单击并按住,移动给定的偏移值,然后释放鼠标。
        actions.dragAndDropBy(id, 0, 0);
        // 右击
        actions.contextClick(id);
        // 在某个元素上右击
        actions.contextClick(driver.findElement(By.id("")));
        // 双击
        actions.doubleClick();
        // 在现有鼠标位置释放按下的鼠标左键
        actions.release();

        // 隐式等待
        driver.manage().timeouts().implicitlyWait(8, TimeUnit.SECONDS);
        // 鼠标移动到设置
        WebElement settings = driver.findElement(By.id("s-usersetting-top"));
        actions.moveToElement(settings).perform();
        // 在设置上显式等待5秒,定位并点击
        WebDriverWait wait = new WebDriverWait(driver, 5);
        WebElement searchSettings = wait.until(ExpectedConditions.presenceOfElementLocated(By.cssSelector("#s-user-setting-menu > div > a.setpref.first > span")));
        searchSettings.click();
        //定位 "搜索语言设置"
        List<WebElement> radios = driver.findElements(By.className("item-setting"));
        System.out.println();
        // 每个选项的状态
        for (int i = 0; i < radios.size(); i++) {
            System.out.println("------------");
            System.out.println("第"+i+"个按钮可见状态"+radios.get(i).isDisplayed());
            System.out.println("第"+i+"个按钮可编辑状态"+radios.get(i).isEnabled());
            System.out.println("第"+i+"个按钮可选择状态"+radios.get(i).isSelected());
            radios.get(i).click();
            Thread.sleep(2000);
        }

        // 单选和复选框
        WebElement gender = driver.findElement(By.name("sex"));
        WebElement select2 = driver.findElement(By.name("select2"));
        WebElement select3 = driver.findElement(By.name("select3"));

        Select se1 = new Select(gender);
        Select se2 = new Select(select2);
        Select se3 = new Select(select3);

        se1.selectByValue("2");
        se2.selectByIndex(5);
        se3.selectByVisibleText("医学");

        // Assert 断言
        Assertion assertion = new Assertion();
        assertion.assertEquals(id ,tag_name);
        // Assert.assertEquals(id, name);


    }
}

如有任何疑问,都可以留言咨询。
暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇