2024全国大学生软件测试大赛-校内练习题-京东、网易(功能)
1.京东
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class Example_jd {
public static void test(WebDriver driver) throws InterruptedException {
// TODO Test script
// eg:driver.get("https://www.baidu.com/")
// eg:driver.findElement(By.id("wd"));
// 3.加载浏览器
// 4.通过chromeDriver打开浏览器
// chromeDriver.get("https://www.baidu.com/");
driver.get("https://www.jd.com/");
Thread.sleep(1500);
driver.manage().window().maximize();
Thread.sleep(1000);
// 1. 打开京东官网首页,选择城市为台湾
Actions actions = new Actions(driver);
WebElement city = driver.findElement(By.id("ttbar-mycity"));
actions.moveToElement(city).perform();
Thread.sleep(1000);
driver.findElement(By.linkText("台湾")).click();
Thread.sleep(1000);
// 2 .搜索框输入“家具”并搜索
driver.findElement(By.id("key")).sendKeys("家具");
Thread.sleep(1500);
driver.findElement(By.xpath("//*[@id=\"search\"]/div/div[2]/button")).click();
Thread.sleep(1000);
driver.findElement(By.id("loginname")).sendKeys("lcc聪聪", Keys.TAB, "lcc20040220");
Thread.sleep(1000);
driver.findElement(By.id("loginsubmit")).click();
// 3 .选择【源氏木语】
WebDriverWait wait = new WebDriverWait(driver, 15);
WebElement yuanShi = wait.until(ExpectedConditions.elementToBeClickable(By.linkText("源氏木语")));
yuanShi.click();
Thread.sleep(1000);
// 4 .选择【边桌/茶几】
driver.findElement(By.linkText("边桌/茶几")).click();
Thread.sleep(1000);
// 5.选择【111-120cm】
driver.findElement(By.linkText("111-120cm")).click();
Thread.sleep(1000);
// 6.选择【实木】
driver.findElement(By.linkText("实木")).click();
Thread.sleep(1000);
// 7.选择【可储物】
driver.findElement(By.linkText("可储物")).click();
Thread.sleep(1000);
// 8. 按顺序点击【新品】【综合】【销量】【评论数】
driver.findElement(By.linkText("新品")).click();
driver.navigate().refresh();
Thread.sleep(1000);
driver.findElement(By.linkText("综合")).click();
driver.navigate().refresh();
Thread.sleep(1000);
driver.findElement(By.linkText("销量")).click();
driver.navigate().refresh();
Thread.sleep(1000);
driver.findElement(By.linkText("评论数")).click();
driver.navigate().refresh();
Thread.sleep(1000);
// 9. 选择第一条信息
driver.findElement(By.xpath("//*[@id=\"warecard_10119671215230\"]")).click();
Thread.sleep(1500);
// 10.选择【茶几原木色1200*600*450mm】
driver.switchTo().window(driver.getWindowHandles().toArray()[1].toString());
WebElement chaJi = wait.until(ExpectedConditions
.elementToBeClickable(By.cssSelector("a[clstag='shangpin|keycount|product|yanse-1.2米茶几']")));
chaJi.click();
Thread.sleep(1500);
// JavascriptExecutor executor = (JavascriptExecutor) driver;
// executor.executeScript("window.scrollTo(0, 400)");
driver.findElement(By.xpath("//*[@id=\"detail\"]/div[1]/ul/li[2]")).click();
Thread.sleep(1500);
driver.findElement(By.xpath("//*[@id=\"detail\"]/div[1]/ul/li[5]")).click();
Thread.sleep(1500);
driver.findElement(By.xpath("//*[@id=\"detail\"]/div[1]/ul/li[6]")).click();
Thread.sleep(1500);
// 12.返回第一个窗口并选择【超市】
driver.switchTo().window(driver.getWindowHandles().toArray()[0].toString());
Thread.sleep(1500);
driver.findElement(By.linkText("超市")).click();
Thread.sleep(1500);
// 13.返回上层窗口并关闭上层窗口
driver.switchTo().window(driver.getWindowHandles().toArray()[2].toString());
driver.close();
Thread.sleep(1500);
// 14.跳转至【超市】页面,关闭【超市】页面
driver.switchTo().window(driver.getWindowHandles().toArray()[1].toString());
driver.close();
Thread.sleep(1500);
}
public static void main(String[] args) {
// Run main function to test your script.
System.setProperty("webdriver.chrome.driver",
"D:\\RuanJian\\google\\chrome-win64\\chrome-win64\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
try {
test(driver);
} catch (Exception e) {
e.printStackTrace();
} finally {
driver.quit();
}
}
}
2.网易
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class Example_wy {
public static void test(WebDriver driver) throws InterruptedException {
// TODO Test script
// eg:driver.get("https://www.baidu.com/")
// eg:driver.findElement(By.id("wd"));
// 3.加载浏览器
// 4.通过chromeDriver打开浏览器https://www.jd.com/
// chromeDriver.get("https://www.baidu.com/");
driver.get("https://www.163.com/");
Thread.sleep(1500);
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.manage().window().maximize();
Thread.sleep(1500);
// 2. 选择【家居】并做【点击】操作
driver.findElement(By.linkText("家居")).click();
driver.switchTo().window(driver.getWindowHandles().toArray()[1].toString());
Thread.sleep(1500);
// 3. 选择【设计师】并做【点击】操作
driver.findElement(By.linkText("设计师")).click();
driver.switchTo().window(driver.getWindowHandles().toArray()[2].toString());
Thread.sleep(3000);
// 4. 选择【家装案例】并做【点击】操作
driver.findElement(By.linkText("家装案例")).click();
driver.switchTo().window(driver.getWindowHandles().toArray()[3].toString());
Thread.sleep(3000);
// 5. 选择【户型】并做【点击】操作
JavascriptExecutor executor = (JavascriptExecutor) driver;
// executor.executeScript("window.scrollTo(0, 600)");
// Thread.sleep(1500);
driver.findElement(By.xpath("//*[@id=\"ne_wrap\"]/body/div[9]/span[2]")).click();
Thread.sleep(1500);
// 6. 选择【三居室】并做【点击】操作
driver.findElement(By.xpath("//*[@id=\"ne_wrap\"]/body/div[10]/div[2]/div/span[6]")).click();
Thread.sleep(1500);
// 7. 选择【搜索】并做【点击】操作
driver.findElement(By.xpath("//*[@id=\"ne_wrap\"]/body/div[9]/span[4]")).click();
Thread.sleep(1500);
// 8. 选择【输入框】并做【输入】操作,输入内容为【现代】
driver.findElement(By.cssSelector("input[placeholder='请输入关键词']")).sendKeys("现代");
Thread.sleep(1500);
// 9. 选择确认【搜索】并做【点击】操作
// driver.findElement(By.xpath("//*[@id=\"ne_wrap\"]/body/div[10]/div[4]/div/div")).click();
driver.findElement(By.className("search")).click();
Thread.sleep(1500);
// 10.关闭当前【页面】选择并移至如图所示【页面】
driver.close();
driver.switchTo().window(driver.getWindowHandles().toArray()[2].toString());
Thread.sleep(2000);
// 11.选择【严选】并做【点击】操作
driver.findElement(By.id("js_lofter_icon_url")).click();
driver.switchTo().window(driver.getWindowHandles().toArray()[3].toString());
Thread.sleep(2000);
// 12.选择【数码家电】并做【点击】操作
driver.findElement(By.linkText("数码家电")).click();
driver.switchTo().window(driver.getWindowHandles().toArray()[4].toString());
Thread.sleep(2000);
// 13.选择【品质尖货】并做【点击】操作
driver.findElement(By.linkText("品质尖货")).click();
Thread.sleep(1500);
// 14.选择【澳门】并做【点击】操作
driver.findElement(By.linkText("澳门")).click();
Thread.sleep(1500);
// 15.选择【价格由高到低小箭头】并做【点击】操作
driver.findElement(By.xpath("//*[@id=\"j-goodsAreaWrap\"]/div/div[2]/div[1]/div[3]/a[2]/div/i[2]")).click();
Thread.sleep(1500);
// 16.选择首条资源【图片】并做【点击】操作(注:页面如有变动,依然选择首张图片资源)
// executor.executeScript("window.scrollTo(0, 300)");
Thread.sleep(6000);
driver.findElement(By.xpath("//*[@id=\"j-goodsAreaWrap\"]/div/div[2]/div[2]/ul/li[1]/div/div/div[1]/a/div/img"))
.click();
driver.switchTo().window(driver.getWindowHandles().toArray()[5].toString());
Thread.sleep(1500);
// 17.选择【评价】并做【点击】操作
// executor.executeScript("window.scrollTo(0, 300)");
WebElement pingJia = driver.findElement(By.xpath("//*[@id=\"detailNavTab\"]/div/div/div/ul/li[2]/span"));
executor.executeScript("window.scrollTo(0, " + pingJia.getLocation().y + ")");
pingJia.click();
Thread.sleep(1500);
// 18.选择【追评】并做【点击】操作
driver.findElement(
By.xpath("//*[@id=\"j-app\"]/div/div[2]/div[3]/div[1]/div[2]/div[1]/ul/div[1]/li/div/div[2]/span[3]"))
.click();
Thread.sleep(1500);
// 19.选择【评价时间】并做【点击】操作
driver.findElement(By.xpath("//*[@id=\"j-app\"]/div/div[2]/div[3]/div[1]/div[2]/div[1]/ul/div[2]/div/a[2]"))
.click();
Thread.sleep(1500);
// 20.选择【回顶部】并做【点击】操作
driver.findElement(By.id("j-yx-fixedtoolGoTop")).click();
Thread.sleep(1500);
// 21.选择【输入框】并做【输入】操作,输入【香薰】
driver.findElement(By.name("searchInput")).sendKeys("香薰");
Thread.sleep(2000);
// 22.选择【搜索】并做【点击】操作
driver.findElement(By.xpath("//*[@id=\"j-yx-cp-m-top\"]/div[2]/div/div[3]/div[2]")).click();
Thread.sleep(2000);
// 23.选择【母婴亲子】并做【点击】操作
driver.findElement(By.xpath("//*[@id=\"j-searchContainer\"]/div/div/div/div[2]/div[1]/div[1]/div/a[1]"))
.click();
Thread.sleep(1500);
// 24.选择【价格输入框】并做【输入】操作,输入内容为【10】
driver.findElement(By.name("floorPrice")).sendKeys("10");
Thread.sleep(1500);
// 25.选择【价格输入框】并做【输入】操作,输入内容为【40】
driver.findElement(By.name("upperPrice")).sendKeys("40");
Thread.sleep(1500);
// 26.选择【确定】并做【点击】操作
driver.findElement(By.xpath("//*[@id=\"j-searchContainer\"]/div/div/div/div[2]/div[1]/div[2]/div/div[3]/a[1]"))
.click();
Thread.sleep(1500);
// 27.选择首条资源【图片】并做【点击】操作
driver.findElement(By.xpath(
"//*[@id=\"j-searchContainer\"]/div/div/div/div[2]/div[2]/div[1]/ul/li/div/div/div[1]/a/div/img"))
.click();
driver.switchTo().window(driver.getWindowHandles().toArray()[6].toString());
Thread.sleep(1500);
// 28.选择【收藏】并做【点击】操作
driver.findElement(By.className("w-collectZone")).click();
Thread.sleep(1500);
}
public static void main(String[] args) {
// Run main function to test your script.
System.setProperty("webdriver.chrome.driver",
"D:\\RuanJian\\google\\chrome-win64\\chrome-win64\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
try {
test(driver);
} catch (Exception e) {
e.printStackTrace();
} finally {
// driver.quit();
}
}
}