selenium-java,解決一些加了顯性等待和隱性等待都不好使的情況,以及給UI自動化加上暫停功能
阿新 • • 發佈:2017-09-13
event until rdo 暫停 ati ntop 面板 -1 不想
最近在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 = newJFrame("開始/暫停");//新建一個窗體,窗體的標題是""中的內容, 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自動化加上暫停功能