นี่คือการรับชื่อเบราว์เซอร์และเวอร์ชันของเบราว์เซอร์พร้อมไดรเวอร์เว็บ Selenium นี่จะส่งคืนชื่อเบราว์เซอร์และชื่อรุ่นเช่น Firefox 29, Internet Explorer 08, Chrome 35
สิ่งนี้ทำกับ javascript และ selenium web driver เพื่อแสดงหมายเลขรุ่น OSDetector ส่งคืนชื่อระบบปฏิบัติการสำหรับ Windows, MAC, Linux และ Solaris
ตรวจจับ OS, เบราว์เซอร์และเวอร์ชั่นโดยใช้ Java และ Selenium WebDriver
import org.openqa.selenium.Capabilities; import org.openqa.selenium.JavascriptExecutor; import org.openqa.selenium.WebDriver; import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.Capabilities; import org.openqa.selenium.JavascriptExecutor; import org.openqa.selenium.WebDriver; import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.Capabilities; import org.openqa.selenium.JavascriptExecutor; import org.openqa.selenium.WebDriver; import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.Capabilities; import org.openqa.selenium.JavascriptExecutor; import org.openqa.selenium.WebDriver; import org.openqa.selenium.remote.RemoteWebDriver;
BrowserVersion ระดับสาธารณะ
WebDriver ส่วนตัวคงที่ browserDriver;
ประชาชนคงที่สตริง getBrowserAndVersion () {
String browser_version = null;
ขีดความสามารถ cap = ((RemoteWebDriver) browserDriver) .getCapabilities ();
String browsername = cap.getBrowserName ();
// บล็อกนี้เพื่อค้นหาหมายเลขเวอร์ชัน IE
if (“ internet explorer” .equalsIgnoreCase (ชื่อเรียกดู)) {
String uAgent = (String) ((JavascriptExecutor) browserDriver) .executeScript (“ return navigator.userAgent;”);
System.out.println (uAgent);
// uAgent ส่งคืนเป็น“ MSIE 8.0 Windows” สำหรับ IE8
if (uAgent.contain (“ MSIE”) && uAgent.contain (“ Windows”)) {
browser_version = uAgent.substring (uAgent.indexOf (“ MSIE”) + 5, uAgent.indexOf (“ Windows”) - 2);
} อื่น ๆ ถ้า (uAgent.contain (“ ตรีศูล / 7.0”)) {
browser_version =“ 11.0”;
} อื่น {
browser_version =“ 0.0”;
}
} อื่น ๆ
{
// เวอร์ชันของเบราว์เซอร์สำหรับ Firefox และ Chrome
browser_version = cap.getVersion (); // .split (“.”) [0];
}
String browserversion = browser_version.substring (0, browser_version.indexOf (“.”));
คืนค่าชื่อผู้ใช้ +”” + Browserversion;
}
String คงสาธารณะ OSDetector () {
String os = System.getProperty (“ os.name”). toLowerCase ();
if (os.contain (“ win”)) {
ส่งคืน“ Windows”;
} else if (os.contain (“ nux”) || os.contain (“ nix”)) {
ส่งคืน“ Linux”;
} อื่น ๆ ถ้า (os.contain (“ mac”)) {
ส่งคืน“ Mac”;
} อื่น ๆ ถ้า (os.contain (“ sunos”)) {
ส่งคืน“ Solaris”;
} อื่น {
ส่งคืน“ อื่น ๆ ”;
}
}
}