其他分享
首页 > 其他分享> > web应用功能测试2——MOOCTEST

web应用功能测试2——MOOCTEST

作者:互联网

web应用功能测试2——MOOCTEST

girlbbs

http://114.215.176.95:60509/girl-bbs/index.jsp

代码

Example.java

import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;

import java.lang.Thread;
import java.util.Random;
public class Example {

    // Mooctest Selenium Example


    // <!> Check if selenium-standalone.jar is added to build path.
	

    public static void test(WebDriver driver) throws Exception {
        // TODO Test script
        // eg:driver.get("https://www.baidu.com/")
        // eg:driver.findElement(By.id("wd"));
    	
    	driver.get("http://114.215.176.95:60509/girl-bbs/index.jsp");
        driver.manage().window().maximize();
        Thread.sleep(3000);
        driver.findElement(By.xpath("/html/body/table[1]/tbody/tr/td/table/tbody/tr[2]/td/table/tbody/tr[1]/td[2]/a/font")).click();
        //login
        Thread.sleep(3000);
        driver.findElement(By.xpath("/html/body/table[1]/tbody/tr/td/table/tbody/tr[2]/td/font/form/table/tbody/tr[3]/td/a/font")).click();
        //back
        Thread.sleep(3000);
        driver.findElement(By.xpath("/html/body/table[1]/tbody/tr/td/table/tbody/tr[2]/td/table/tbody/tr[3]/td[2]/a/font")).click();
        //register
        Thread.sleep(3000);
        
        String usernames = names.getAlphanumString(8);
        driver.findElement(By.xpath("/html/body/table[1]/tbody/tr/td/table/tbody/tr[2]/td/form/table/tbody/tr[2]/td[2]/input")).sendKeys(usernames);
        //username
        String passwords = names.getAlphanumString(5);
        driver.findElement(By.xpath("/html/body/table[1]/tbody/tr/td/table/tbody/tr[2]/td/form/table/tbody/tr[3]/td[2]/input")).sendKeys(passwords);
        //password
        
        driver.findElement(By.xpath("/html/body/table[1]/tbody/tr/td/table/tbody/tr[2]/td/form/table/tbody/tr[4]/td[2]/input")).sendKeys(passwords);
        //passconfirm
        
        WebElement selector = driver.findElement(By.xpath("/html/body/table[1]/tbody/tr/td/table/tbody/tr[2]/td/form/table/tbody/tr[5]/td[2]/select"));
        //下拉框
        Actions ac = new Actions(driver); 
        ac.moveToElement(selector).perform();
        //悬停
        driver.findElement(By.xpath("/html/body/table[1]/tbody/tr/td/table/tbody/tr[2]/td/form/table/tbody/tr[5]/td[2]/select/option[2]")).click();
        //felmale
        driver.findElement(By.xpath("/html/body/table[1]/tbody/tr/td/table/tbody/tr[2]/td/form/table/tbody/tr[6]/td[2]/input")).sendKeys("1234567");
        //phone
        driver.findElement(By.xpath("/html/body/table[1]/tbody/tr/td/table/tbody/tr[2]/td/form/table/tbody/tr[7]/td[2]/input")).sendKeys("antique.@163.com");
        //email
        Thread.sleep(3000);
        //driver.findElement(By.xpath("/html/body/table[1]/tbody/tr/td/table/tbody/tr[2]/td/form/table/tbody/tr[8]/td[2]/input[1]")).click();
        driver.findElement(By.name("submit")).click();
        //submit
        Thread.sleep(3000);
        Alert alert1 = driver.switchTo().alert();
        //alert
        Thread.sleep(3000);
        alert1.accept();
        //confirm
        driver.findElement(By.xpath("/html/body/center[2]/form/input[1]")).sendKeys("test");
        //input "test"
        Thread.sleep(3000);
        driver.findElement(By.xpath("/html/body/center[2]/form/input[2]")).click();
        //search
        Thread.sleep(3000);
        driver.findElement(By.xpath("/html/body/center/center[2]/a[2]")).click();
        //back
        Thread.sleep(3000);
        driver.findElement(By.xpath("/html/body/center[2]/a[2]")).click();
        //leave us a message
        Thread.sleep(3000);
        driver.findElement(By.xpath("/html/body/table[1]/tbody/tr/td/table/tbody/tr[2]/td/form/table/tbody/tr[1]/td/input\n")).clear();
        driver.findElement(By.xpath("/html/body/table[1]/tbody/tr/td/table/tbody/tr[2]/td/form/table/tbody/tr[1]/td/input\n")).sendKeys("主题1");
        //input "theme"
        driver.findElement(By.xpath("/html/body/table[1]/tbody/tr/td/table/tbody/tr[2]/td/form/table/tbody/tr[3]/td/textarea")).sendKeys("内容1");
        //input "content"
        Thread.sleep(3000);
        driver.findElement(By.xpath("/html/body/table[1]/tbody/tr/td/table/tbody/tr[2]/td/form/table/tbody/tr[4]/td/input")).click();
        //submit
        Thread.sleep(3000);
        driver.findElement(By.xpath("/html/body/center[2]/a[1]")).click();
        //logout
        Thread.sleep(3000);
        Alert alert2 = driver.switchTo().alert();
        //alert
        Thread.sleep(3000);
        alert2.accept();
        //confirm
    }

    public static void main(String[] args) {
        // Run main function to test your script.
    	System.setProperty("webdriver.chrome.driver","C:\\webDriver\\chromedriver.exe");
        WebDriver driver = new ChromeDriver();
        try { test(driver); } 
        catch(Exception e) { e.printStackTrace(); }
        finally { driver.quit(); }
    }

}

names.java(此处是复制的别的博客代码,忘记是哪篇了,侵删)

import java.util.Random;

public class names {
	public static String getAlphanumString(int length) {
		Random random = new Random();
		StringBuffer buf = new StringBuffer();
		String charStr = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
		int charLength = charStr.length();
		for (int i = 0; i < length; i++) {
		int index = random.nextInt(charLength);
		buf.append(charStr.charAt(index));
		}
		return buf.toString();
		}
}

运行结果

赛前练习,记得好像是70多分,没有截图。

标签:web,driver,tr,tbody,功能测试,findElement,table,td,MOOCTEST
来源: https://blog.csdn.net/m0_52541919/article/details/121053268