您好,欢迎来到二三娱乐。
搜索
您的当前位置:首页Appium踩坑与Appium常用代码

Appium踩坑与Appium常用代码

来源:二三娱乐

之前log一直显示是,

An element could not be located on the page using the given search parameters. (WARNING: The server did not provide any stacktrace information)

元素定位不到,这让我的纠错方向一下就变了,一直是以为我的元素定位方法不对,后来发现是因为页面加载不完全,所以不能定位到,所以需要加个等待。
Appium常用的wait等待有三个代码。


image.png
  • 线程休眠
    强制等待60s
Thread.sleep(60000)
  • 隐式等待
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
  • 显示等待
    等待id为q的元素加载完毕
WebDriverWait wait = new WebDriverWait(driver, 60);
    WebElement e= wait.until(new  ExpectedCondition<WebElement>() {
            @Override
            public WebElement apply(WebDriver d) {
                return d.findElement(By.id("q"));
            }
        })

其他干货

  • 检查网络
/***
* 检查网络
* @return 是否正常
*/
public static boolean checkNet(){
String text=driver.getNetworkConnection().toString();
if(text.contains("Data: true"))
return true;
else
return false;
}
  • 打印整个页面PageSource
System.out.print(driver.getPageSource());
  • 获取当前时间并截图,命名
public static String getScreen(){
String fileRoute="路径";
SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmm");
String picname=fileRoute+df.format(new Date()).toString()+".png";
       File screen = driver.getScreenshotAs(OutputType.FILE);
       System.out.println(picname);
       File screenFile = new File(picname);
       try {
           FileUtils.copyFile(screen, screenFile); 
           String time=df.format(new Date()).toString();
           System.out.println("当前时间"+time);
           return time;
       } catch (IOException e) {
           e.printStackTrace();
       }
return null;
}
  • 发送邮件:HTML/TXT(要把图片放到IIS路径拼接url)
public void mail(String Value, String subject ) {
String smtpHost ="172.17.1.23";
String from = "mail";
String to = "mail";
String subject = value2; //subject javamail自动转码
StringBuffer theMessage = new StringBuffer();
theMessage.append("<h2><font color=red>**截图:</font></h2>");
theMessage.append("<hr>");
theMessage.append(Value);
try {
Mail.sendMessage(smtpHost, from, to, subject, theMessage.toString());
}
catch (javax.mail.MessagingException exc) {
exc.printStackTrace();
}
catch (java.io.UnsupportedEncodingException exc) {
exc.printStackTrace();
}
}

``` java
public static void sendMessage(String smtpHost,String from, String to, String subject, String messageText)throws MessagingException,java.io.UnsupportedEncodingException
{
// Step  :  Configure the mail session
System.out.println("Configuring mail session for: " + smtpHost);
java.util.Properties props = new java.util.Properties();
props.setProperty("mail.smtp.auth", "true");//指定是否需要SMTP验证
props.setProperty("mail.smtp.host", smtpHost);//指定SMTP服务器
props.put("mail.transport.protocol", "smtp");
Session mailSession = Session.getDefaultInstance(props);
mailSession.setDebug(true);//是否在控制台显示debug信息
// Step  :  Construct the message
System.out.println("Constructing message -  from=" + from + "  to=" + to);
InternetAddress fromAddress = new InternetAddress(from);
InternetAddress toAddress = new InternetAddress(to);
MimeMessage testMessage = new MimeMessage(mailSession);
testMessage.setFrom(fromAddress);
testMessage.addRecipient(javax.mail.Message.RecipientType.TO, toAddress);
testMessage.setSentDate(new java.util.Date());
testMessage.setSubject(MimeUtility.encodeText(subject,"gb2312","B"));
testMessage.setContent(messageText, "text/html;charset=gb2312");
System.out.println("Message constructed");
// Step  :  Now send the message
Transport transport = mailSession.getTransport("smtp");
transport.connect(smtpHost, "mail", "passwd");
transport.sendMessage(testMessage, testMessage.getAllRecipients());
transport.close();
System.out.println("Message sent!");
}

Copyright © 2019- yule263.com 版权所有 湘ICP备2023023988号-1

违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务