如何讓Selenium 使用之前的一個session

需求: 

如何在新啓動的應用, 使用上一次應用打開的那個瀏覽器?

由於每次啓動應用, 都會全新打開一個新的瀏覽器, 在一個新的會話下進行工作.

Solution:

由於之前使用的是Session來進行通信的, 那麼可以使用上一次會話的sessionid

具體解決方案:

  1. 使用RemoteWebDriver的方式進行創建Selenium服務.
  • 將chromedriver.exe 和 selenium-server-standalone-3.3.1.jar 放在同一個文件夾內
  • 使用 java -jar selenium-server-standalone-3.3.1.jar 命令啓動Selenium服務.
  • 打開http://localhost:4444/wd/hub/static/resource/hub.html 查看,如果是下面的界面, 則表示Selenium服務啓動成功.

2.  創建RemoteWebDriver的實現類,

  • 由於RemoteWebDriver類中的setSessionID是Protected的, 無法直接調用, 所以創建子類進行擴展.
  • 創建子類 MyWebDriver, 並將setSessionId置爲 public
			public class MyWebDriver extends RemoteWebDriver {
			
			    public MyWebDriver(URL url, DesiredCapabilities cap) {
			        super(url, cap);
			    }
			
			    @Override
			    public void setSessionId(String opaqueKey) {
			        super.setSessionId(opaqueKey);
			    }
			}
  • 使用的時候, 使用之前的那個sessionid進行使用, 例如 訪問Baidu.

			@Test
			public void usingOpenedPageForAccessBaidu() throws MalformedURLException {
			    String sessinId = "b0c01669-624f-4b9f-9ed4-e8419492f1891";
			    DesiredCapabilities cap = DesiredCapabilities.chrome();
			    cap.setPlatform(Platform.ANY);
			    String urlStr = "http://localhost:4444/wd/hub";
			      RemoteWebDriver driver=new RemoteWebDriver(new URL(urlStr),cap);
			    MyWebDriver driver = new MyWebDriver(new URL(urlStr), cap);
			    String newSessionId = driver.getSessionId().toString();
			    try {
			        driver.setSessionId(sessinId);
			        String url = driver.getCurrentUrl();
			        driver.get("http://www.baidu.com");
			        driver.findElement(By.cssSelector("#kw")).sendKeys("hello");
			        driver.findElement(By.cssSelector("#su")).click();
			    } catch (Exception e) {
			        e.printStackTrace();
			    } finally {
			        driver.setSessionId(newSessionId);
			        driver.quit();
			    }
			}
  • 例子2, 關閉所有已經打開的Session
			@Test
			public void getAllSessionAndCloseIt() throws MalformedURLException {
			    List<String> sessionList = new ArrayList<>();
			    DesiredCapabilities cap = DesiredCapabilities.chrome();
			    cap.setPlatform(Platform.ANY);
			    String urlStr = "http://localhost:4444/wd/hub";
			    MyWebDriver driver = new MyWebDriver(new URL(urlStr), cap);
			    driver.get("http://localhost:4444/wd/hub/static/resource/hub.html");
			    List<WebElement> eles = driver.findElements(By.cssSelector(".session-container>.control-block>button"));
			    WebElement refreshBtnEle = eles.get(1); //Click the refeash button
			    refreshBtnEle.click();
			    List<WebElement> tbsEles = driver.findElements(By.cssSelector(".goog-tab"));
			    for (WebElement tbsEle : tbsEles) {
			        tbsEle.click();
			        WebElement sessionLabel = driver.findElements(By.cssSelector(".control-block span")).get(0);
			        String session = sessionLabel.getText();
			        System.out.println("session----->:" + session);
			        sessionList.add(session);
			    }
			    for (String session : sessionList) {
			        driver.setSessionId(session);
			        driver.quit();
			    }
			}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章