Selenium Data Driven Testing

This case will use the Testng framework

Data is provided by .csv file

Let's look directly at the code:

package selenium_01;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

public class Sjqd {
	 WebDriver dr;
	  String URL="http:\\192.168.8.200";
	@Test(dataProvider="sj")
	  public void f(String user,String password) throws InterruptedException {
		dr.get(URL);
		dr.findElement(By.id("zentao")).click();
		Thread.sleep(1000);
		dr.findElement(By.id("account")).sendKeys(user);
		dr.findElement(By.name("password")).sendKeys(password);
		dr.findElement(By.id("submit")).click();
		Thread.sleep(2000);
		//The following steps are used to determine whether the login is successful or not.
		String aString=dr.getCurrentUrl();
		String s="my";
		if (aString.contains(s)) {
			System.out.println("User name: "+user+" password: "+password+" user successfully logged in");
		}else {
			System.out.println("User name: "+user+" password: "+password+" user login failed");
		}
		dr.get(URL);
		Thread.sleep(3000);
		}
	@DataProvider(name="sj")
	public Object[][] Data() throws IOException{
		return CSV();
	}
	
	  @BeforeTest
	  public void beforeTest() {
		  System.setProperty("webdriver.chrome.driver","C:\\Users\\Administrator\\Desktop\\Browser\\Browser Driver\\chromedriver.exe");
		  dr=new ChromeDriver();
		  dr.manage().window().maximize();
	  }

	  @AfterTest
	  public void afterTest() {
		  dr.quit();
	  }
	  
	  
	  //This method is used to read a csv file and store the data in the file in a two-dimensional array
	  public static Object[][] CSV() throws IOException {
			ArrayList<Object> list=new ArrayList<Object>();
			
			BufferedReader in=new BufferedReader(new FileReader("C:\\Users\\Administrator\\Desktop\\Data.csv"));
			in.readLine();
			String a=null;
			while((a=in.readLine())!=null) {
				String b[]=a.split(",");
				list.add(b);
			
			}
			in.close();
	         Object[][] ob=new Object[list.size()][];
	         for(int i=0;i<list.size();i++) {
	        	 ob[i]=(Object[]) list.get(i);
	         }
			return ob;
	         
			
		}
	  
	  
	  
	  
}


You can see that Neon is relatively rough here, the pop-up box has not been processed, and there are many places that have not been optimized. We can also use the page design pattern to encapsulate, extract, and so on.

The main purpose here is to make it easier for everyone to understand and understand.

Everyone, look at the code and think about it. In fact, it is easy to understand. The only thing that is difficult to understand is the two-dimensional array, so everyone should always review the basic knowledge. Haha welcome everyone to point out the shortcomings of Neon, let's make progress together.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326016692&siteId=291194637