| package appium_operate; import java.net.MalformedURLException; import java.net.URL; import java.util.concurrent.TimeUnit; import org.openqa.selenium.By; import org.openqa.selenium.remote.DesiredCapabilities; import org.testng.Assert; import org.testng.annotations.BeforeTest; import org.testng.annotations.DataProvider; import org.testng.annotations.Test; import io.appium.java_client.android.AndroidDriver; /** * @author 李小卫 E-mail:yyymlxw@163.com @date 创建时间2018年2月11日上午10:30:02 */ public class CalcTest { AndroidDriver driver; @BeforeTest public void setUp() throws MalformedURLException{ DesiredCapabilities des = new DesiredCapabilities(); // des.setCapability("app", "c:\\"); des.setCapability("platformName", "Android"); des.setCapability("platformVersion", "4.4"); des.setCapability("udid", "192.168.43.101:5555"); des.setCapability("deviceName", "s4"); des.setCapability("appPackage", "com.android.calculator2");//com.android.contacts des.setCapability("appActivity", ".Calculator");//.activities.PeopleActivity des.setCapability("unicodeKeyboard", "True"); des.setCapability("resetKeyboard", "True"); des.setCapability("newCommandTimeout", "15"); des.setCapability("nosign", "True"); driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"),des); driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); } @Test(enabled = false) public void add() { driver.findElement(By.xpath("//android.widget.Button[@text='5']")).click(); driver.findElement(By.xpath("//android.widget.Button[@text='+']")).click(); driver.findElement(By.xpath("//android.widget.Button[@text='8']")).click(); driver.findElement(By.xpath("//android.widget.Button[@text='=']")).click(); String value = driver.findElement(By.xpath("//android.widget.EditText[@class='android.widget.EditText']")).getAttribute("text"); Assert.assertEquals(value, "13"); } @Test(enabled = false) public void sub() { driver.findElement(By.xpath("//android.widget.Button[@text='1']")).click(); driver.findElement(By.xpath("//android.widget.Button[@text='0']")).click(); driver.findElement(By.xpath("//android.widget.Button[@text='-']")).click(); driver.findElement(By.xpath("//android.widget.Button[@text='8']")).click(); driver.findElement(By.xpath("//android.widget.Button[@text='=']")).click(); String value = driver.findElement(By.xpath("//android.widget.EditText[@class='android.widget.EditText']")).getAttribute("text"); Assert.assertEquals(value, "2"); } @Test(enabled = false) public void mul() { driver.findElement(By.xpath("//android.widget.Button[@text='5']")).click(); driver.findElement(By.xpath("//android.widget.Button[@text='×']")).click(); driver.findElement(By.xpath("//android.widget.Button[@text='8']")).click(); driver.findElement(By.xpath("//android.widget.Button[@text='=']")).click(); String value = driver.findElement(By.xpath("//android.widget.EditText[@class='android.widget.EditText']")).getAttribute("text"); Assert.assertEquals(value, "40"); } @DataProvider(name="testdata") public Object[][] getData(){ return new Object[][]{{"20","80","100","+"},{"90","3","270","×"},{"6","2","3","÷"}}; } @Test(dataProvider = "testdata") public void calcTestcase(String num1,String num2,String result,String calcType){ for(char num:num1.toCharArray()){ driver.findElement(By.xpath("//android.widget.Button[@text='"+String.valueOf(num)+"']")).click(); } driver.findElement(By.xpath("//android.widget.Button[@text='"+calcType+"']")).click(); for(char num:num2.toCharArray()){ driver.findElement(By.xpath("//android.widget.Button[@text='"+String.valueOf(num)+"']")).click(); } driver.findElement(By.xpath("//android.widget.Button[@text='=']")).click(); String value = driver.findElement(By.xpath("//android.widget.EditText[@class='android.widget.EditText']")).getAttribute("text"); Assert.assertEquals(value, result); } } |