selenium-java,解决一些加了显性等待和隐性等待都不好使的情况,以及给UI自动化加上暂停功能
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了selenium-java,解决一些加了显性等待和隐性等待都不好使的情况,以及给UI自动化加上暂停功能相关的知识,希望对你有一定的参考价值。
最近在UI自动化时遇到了,上一步成功操作后没有响应的情况(动画加载和浏览器加载导致实际没有问题),导致下一步无法成功操作,所有想在尝试2次操作后再次进行上一步操作解决这种情况导致的错误(其实是不想每一次有问题都要手动加线程沉睡)
1.java应用程序,暂停/开始按钮
import java.awt.Dimension; import java.awt.Toolkit; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JScrollPane; import org.openqa.selenium.WebDriver; import base_operation.Login; @SuppressWarnings({"static-access","deprecation"}) public class Common_method {public void paused_start(Element_operation element_operation){ JFrame frame = new JFrame("开始/暂停");//新建一个窗体,窗体的标题是""中的内容, frame.setSize(200,100);//设置窗体的尺寸、Dimension frameSize=frame.getSize(); Dimension screenSize=Toolkit.getDefaultToolkit().getScreenSize();//获取屏幕尺寸对象 //frame.setLocation((screenSize.width-frame.getWidth())/2, (screenSize.height-frame.getHeight())/2);//设置窗体坐标 frame.setLocation(screenSize.width-220, screenSize.height-150); frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE); //定义了点击关闭按钮的作用; frame.setAlwaysOnTop(true);//把应用置于最前 frame.setVisible(true);//注意这一步一定要调用,使得这个窗体可见,false是不可见; //JPanel panel = new JPanel();//Jpanel 是面板容器类,包含在swing中; JButton jButton1 = new JButton("暂停");//定义一个button frame.add(jButton1); jButton1.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { if(element_operation.get_paused_start()==0){ element_operation.paused(); System.out.println("暂停--------------------------------------------------"); jButton1.setText("开始"); }else if(element_operation.get_paused_start()==1){ element_operation.start(); System.out.println("开始--------------------------------------------------"); jButton1.setText("暂停"); } } }); } }
2.操作方法
import org.openqa.selenium.By; import org.openqa.selenium.NoSuchElementException; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.support.ui.ExpectedCondition; import org.openqa.selenium.support.ui.ExpectedConditions; import org.openqa.selenium.support.ui.WebDriverWait; import io.appium.java_client.android.AndroidDriver; public class Element_operation { private By[] Webelement = {By.className("初始化"),By.className("初始化")}; private String[] Webctype = {"click","click"}; private String[] Webcparameter = {"初始化","初始化"}; private By[] Androidelement = {By.className("初始化"),By.className("初始化")}; private String[] Androidctype = {"click","click"}; private String[] Androidcparameter = {"初始化","初始化"}; public int paused_start = 0; public String return_text = ""; public static String click = "click"; public static String sendKeys = "sendKeys"; public static String getAttribute = "getAttribute"; public static String getText = "getText"; public int get_paused_start(){ //返回是否暂停,0开始,1暂停 return paused_start; } public void paused(){ //暂停 paused_start = 1; } public void start(){ //开始 paused_start = 0; } public String get_return_text(){ //返回值 return return_text; } /* * 记录并操作元素,优化过的等待并操作 * 1.上一步记录到数组0,这一步记录到数组1 * 2.尝试操作元素(这一步) * 3.等待3秒后再尝试点击 * 4.进行上一步操作 * 5.循环2、3、4 * 6.循环3次后不再循环,抛错 */ public void Web_Wait_Element(WebDriver WebDriver,By By,String type,String parameter){ Web_Change(By,type,parameter); boolean open = true; int i = 0; int k = 3;//决定循环次数 while (open) { try { Web_Operate(WebDriver, By, Webctype, parameter, 1); open = false; } catch (Exception e) { try { Thread.sleep(5000);//等待5秒后再次操作 Web_Operate(WebDriver, By, Webctype, parameter, 1); open = false; } catch (Exception e2) { i++; if(i==k){ open = false; System.out.println("这个玩意好顽固3次都搞不定--》"+By+"."+type+"(+"+parameter+")"); Web_Operate(WebDriver, By.id("错误"), Webctype, parameter, 0); } if(i==(k-1)){ System.out.println("进行了一次上一步操作---------------------------"); try { Web_Operate(WebDriver, By, Webctype, parameter, 0); } catch (Exception e3) { // TODO: handle exception System.out.println("上一步不存在--------------------------------"); } } } } } } /* * 对元素进行操作操作 * 参数 * driver、By.findElement(By.???)、操作类型如click、操作参数如sendKeys("???")、上一步0这一步1 */ public void Web_Operate(WebDriver WebDriver,By By,String[] ctype,String parameter,int i){ while(paused_start==1){//判断是否暂停了 try { Thread.sleep(1000); } catch (Exception e) { // TODO: handle exception System.out.println("线程沉睡失败----------------------------------------------------------------"); } } System.out.println("\n定位-->"+Webelement[1]+"\n操作-->"+Webctype[1]+"\n参数-->"+Webcparameter[1]); WebDriverWait wait = new WebDriverWait(WebDriver, 10); wait.until(ExpectedConditions.visibilityOfElementLocated(By));//确定元素是否可见 while(paused_start==1){//判断是否暂停了 try { Thread.sleep(1000); } catch (Exception e) { // TODO: handle exception System.out.println("线程沉睡失败----------------------------------------------------------------"); } } switch (ctype[i]) { case "click": WebDriver.findElement(By).click(); break; case "sendKeys": WebDriver.findElement(By).sendKeys(parameter); break; case "getAttribute": return_text = WebDriver.findElement(By).getAttribute(parameter); break; case "getText": return_text = WebDriver.findElement(By).getText(); break; default: break; } } /* * 记录操作,只记录2步,上一步0和这一步1 * 参数 * By.findElement(By.???)、操作类型如click、操作参数如sendKeys("???") */ public void Web_Change(By By,String type,String parameter){ By change; change = Webelement[1]; Webelement[1] = By; Webelement[0] = change; String change2; change2 = Webctype[1]; Webctype[1] = type; Webctype[0] = change2; String change3; change3 = Webcparameter[1]; Webcparameter[1] = parameter; Webcparameter[0] = change3; } }
使用示例:
public class Guest_Share_Test { private WebDriver WebDriver; private Element_operation element_operation; private Channel channel; private By input1 = By.id("kw"); private By button1 = By.id("su"); @Before public void setUp() throws Exception { element_operation = new Element_operation(); System.setProperty("webdriver.chrome.driver","D:\\Configuration\\chromedriver.exe");//这一步必不可少 WebDriver = new ChromeDriver(); Common_method common_method = new Common_method(); common_method.paused_start(element_operation);//开始/暂停应用 WebDriver.manage().window().maximize(); WebDriver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS); } @After public void tearDown() throws Exception { try { Thread.sleep(1000); } catch (Exception e) { // TODO: handle exception } WebDriver.quit(); } @Test public void test() { WebDriver.get("http://www.baidu.com"); element_operation.Web_Wait_Element(WebDriver, input1, element_operation.sendKeys, "QQ"); element_operation.Web_Wait_Element(WebDriver, input1, element_operation.getText, ""); String text = element_operation.get_return_text();
System.out.println(text); element_operation.Web_Wait_Element(WebDriver, button1, element_operation.click, "");
}
}
以上是关于selenium-java,解决一些加了显性等待和隐性等待都不好使的情况,以及给UI自动化加上暂停功能的主要内容,如果未能解决你的问题,请参考以下文章