51Testing软件测试论坛
标题: 基于C#的Appium自动化测试框架(I):程序结构 [打印本页]
作者: 乐哈哈yoyo 时间: 2017-6-20 10:41
标题: 基于C#的Appium自动化测试框架(I):程序结构
因为工作原因,使用的编程语言都是C#,但是国内相应的Appium资料少得可怜,Java版本的Appium也考虑过,但是奈何自己搞不定Eclipse这个编译环境【说白了就是因为懒……
无意中看到了外面的世界,然后发现国外的相关资料还是很多的,那还说啥?赶紧的,撸起袖子就是干。
前置条件:APPIUM环境搭建,Windows环境下的搭建教程还是很多的,按照教程操作就可以了。
测试环境:Visual Studio 2015+Genymotion模拟器+Custome Phone 6.0.0
源码下载后先设置VS能自动去下载对应的Nuget包,更新的时候有条件的还是可以挂点什么东西,原因你懂的。更新完对应的Nuget包,该关联的引用也都关联上了,报的错也就自行消失了。
顺带真的想吐槽笔记本小屏幕还要强行追求1920*1080的高分辨率,强行放大字体后截图看起来感觉怪怪的,调低分辨率就是满屏幕的马赛克……
我们先来看一段GitHub上的示范代码,这段代码的功能是设置Appium的相关参数,对Windows自带的计算器功能进行测试,测试结果则是调用了Nunit单元测试中的断言功能进行判断:
- public class BasicScenarios
- {
- protected const string WindowsApplicationDriverUrl = "http://127.0.0.1:4723/wd/hub";
- protected static RemoteWebDriver CalculatorSession;
- protected static RemoteWebElement CalculatorResult;
- protected static string OriginalCalculatorMode;
- [ClassInitialize]
- public static void Setup(TestContext context)
- {
- // Launch the calculator app
- DesiredCapabilities appCapabilities = new DesiredCapabilities();
- appCapabilities.SetCapability("app", "Microsoft.WindowsCalculator_8wekyb3d8bbwe!App");
- appCapabilities.SetCapability("platformName", "Windows");
- appCapabilities.SetCapability("deviceName", "WindowsPC");
- CalculatorSession = new RemoteWebDriver(new Uri(WindowsApplicationDriverUrl), appCapabilities);
- Assert.IsNotNull(CalculatorSession);
- CalculatorSession.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(2));
- // Make sure we're in standard mode
- CalculatorSession.FindElementByXPath("//Button[starts-with(@Name, \"Menu\")]").Click();
- OriginalCalculatorMode = CalculatorSession.FindElementByXPath("//List[@AutomationId=\"FlyoutNav\"]//ListItem[@IsSelected=\"True\"]").Text;
- CalculatorSession.FindElementByXPath("//ListItem[@Name=\"Standard Calculator\"]").Click();
- // Use series of operation to locate the calculator result text element as a workaround
- // We currently cannot query element by automationId without using modified appium dot net driver
- // TODO: Use a proper appium/webdriver nuget package that allow us to query based on automationId
- CalculatorSession.FindElementByXPath("//Button[@Name=\"Clear\"]").Click();
- CalculatorSession.FindElementByXPath("//Button[@Name=\"Seven\"]").Click();
- CalculatorResult = CalculatorSession.FindElementByName("Display is 7 ") as RemoteWebElement;
- Assert.IsNotNull(CalculatorResult);
- }
- [ClassCleanup]
- public static void TearDown()
- {
- // Restore original mode before closing down
- CalculatorSession.FindElementByXPath("//Button[starts-with(@Name, \"Menu\")]").Click();
- CalculatorSession.FindElementByXPath("//ListItem[@Name=\"" + OriginalCalculatorMode + "\"]").Click();
- CalculatorResult = null;
- CalculatorSession.Dispose();
- CalculatorSession = null;
- }
- [TestInitialize]
- public void Clear()
- {
- CalculatorSession.FindElementByName("Clear").Click();
- Assert.AreEqual("Display is 0 ", CalculatorResult.Text);
- }
- [TestMethod]
- public void Addition()
- {
- CalculatorSession.FindElementByName("One").Click();
- CalculatorSession.FindElementByName("Plus").Click();
- CalculatorSession.FindElementByName("Seven").Click();
- CalculatorSession.FindElementByName("Equals").Click();
- Assert.AreEqual("Display is 8 ", CalculatorResult.Text);
- }
- }
复制代码从上面的代码我们可以得出,C#下的Appium可以分为以下三个步骤:
1.实例化DesiredCapabilities类并调用SetCapability方法设置对应的一些参数,对应的参数设置可以参考Java版的设置:
- appCapabilities.setappCapabilitiesability("automationName", "Appium");//appium做自动化
- //appCapabilities.setappCapabilitiesability("app", "C:\\software\\jrtt.apk");//安装apk
- //appCapabilities.setappCapabilitiesability("browserName", "chrome");//设置HTML5的自动化,打开谷歌浏览器
- appCapabilities.setappCapabilitiesability("deviceName", "S4");//设备名称
- appCapabilities.setappCapabilitiesability("platformName", "Android"); //安卓自动化还是IOS自动化
- appCapabilities.setappCapabilitiesability("platformVersion", "4.4"); //安卓操作系统版本
- appCapabilities.setappCapabilitiesability("udid", "192.168.56.101:5555"); //设备的udid (adb devices 查看到的)
- appCapabilities.setappCapabilitiesability("appPackage","com.android.calculator2");//被测app的包名
- appCapabilities.setappCapabilitiesability("appActivity",".Calculator");//被测app的入口Activity名称
- appCapabilities.setappCapabilitiesability("unicodeKeyboard", "True"); //支持中文输入
- appCapabilities.setappCapabilitiesability("resetKeyboard", "True"); //支持中文输入,必须两条都配置
- appCapabilities.setappCapabilitiesability("noSign", "True"); //不重新签名apk
- appCapabilities.setappCapabilitiesability("newCommandTimeout", "30");
复制代码
2.设置Appium的服务地址:具体的服务地址是在Appium中进行设置的,同时连接手机,我们把设置好的地址复制过来就可以了。
- <span style="color: #0000ff">new</span> RemoteWebDriver(<span style="color: #0000ff">new</span> Uri(<span style="color: #800000">"</span><span style="color: #800000">http://172.xx.xx.169:4723/wd/hub</span><span style="color: #800000">"</span>), capabilities);
复制代码
3.调用RemoteWebDriver的类下提供的元素定位方法进行元素定位,并对应为好的元素进行对应的操作:Appium提供了以下定位元素的方法(这里的具体语法和Java相比有点区别,需要注意下):
- <span style="color: #000000">FindElement(By by);
- FindElementByClassName(</span><span style="color: #0000ff">string</span><span style="color: #000000"> className);
- FindElementByCssSelector(</span><span style="color: #0000ff">string</span><span style="color: #000000"> cssSelector);
- FindElementById(</span><span style="color: #0000ff">string</span><span style="color: #000000"> id);
- FindElementByLinkText(</span><span style="color: #0000ff">string</span><span style="color: #000000"> linkText);
- FindElementByName(</span><span style="color: #0000ff">string</span><span style="color: #000000"> name);
- FindElementByPartialLinkText(</span><span style="color: #0000ff">string</span><span style="color: #000000"> partialLinkText);
- FindElementByTagName(</span><span style="color: #0000ff">string</span><span style="color: #000000"> tagName);
- FindElementByXPath(</span><span style="color: #0000ff">string</span> xpath);
复制代码
同时,Appium也提供了以下的操作:
- <span style="color: #0000ff">bool</span> Displayed { <span style="color: #0000ff">get</span>; } <span style="color: #008000">//</span><span style="color: #008000">判断元素是否存在</span>
- <span style="color: #0000ff">bool</span> Enabled { <span style="color: #0000ff">get</span>; } <span style="color: #008000">//</span><span style="color: #008000">判断元素是否启用</span>
- <span style="color: #0000ff">bool</span> Selected { <span style="color: #0000ff">get</span>; }<span style="color: #008000">//</span><span style="color: #008000">判断元素是否被选中</span>
- Size Size { <span style="color: #0000ff">get</span>; }<span style="color: #008000">//</span><span style="color: #008000">获取元素的高度和宽度</span>
- <span style="color: #0000ff">string</span> TagName { <span style="color: #0000ff">get</span>; }<span style="color: #008000">//</span><span style="color: #008000">获取元素的文本(不含空格)</span>
- <span style="color: #0000ff">string</span> Text { <span style="color: #0000ff">get</span>; }<span style="color: #008000">//</span><span style="color: #008000">获取指定属性的值</span>
- <span style="color: #0000ff">void</span> Clear();<span style="color: #008000">//</span><span style="color: #008000">清除元素内容</span>
- <span style="color: #0000ff">void</span> Click();<span style="color: #008000">//</span><span style="color: #008000">点击</span>
- <span style="color: #0000ff">string</span> GetAttribute(<span style="color: #0000ff">string</span><span style="color: #000000"> attributeName);
- </span><span style="color: #0000ff">string</span> GetCssValue(<span style="color: #0000ff">string</span> propertyName);<span style="color: #008000">//</span><span style="color: #008000">获取CSS属性的值</span>
- <span style="color: #0000ff">void</span> SendKeys(<span style="color: #0000ff">string</span> text);<span style="color: #008000">//</span><span style="color: #008000">将文本写入元素</span>
- <span style="color: #0000ff">void</span> Submit();<span style="color: #008000">//</span><span style="color: #008000">提交此元素到服务器</span>
复制代码
作者: 草帽路飞UU 时间: 2017-6-20 10:52
很详细,学习了!
作者: 乐哈哈yoyo 时间: 2017-6-20 10:53
加油!
作者: gofengling 时间: 2017-6-23 15:43
好像是测试APP的,是吗?
欢迎光临 51Testing软件测试论坛 (http://bbs.51testing.com/) |
Powered by Discuz! X3.2 |