我有一个Java硒代码,可以运行并从cookie中获取sessionID,它看起来像这样-ASGTFF-node-1 / 2/3/4。这些节点是4台服务器。 现在,在第一次运行中,我需要提取节点号,并通过传递相同的sessionID来使相同的代码针对所有其他3个节点再次循环运行。 例如 - ASGTFF-node-1:这是我第一次跑步时发生的。现在,我需要传递相同的内容,但使用不同的节点号,例如ASGTFF-node-2,ASGTFF-node-3和ASGTFF-node-4。
由于来自sessionID的节点号每次都不同,我该如何循环呢?
这是我尝试过的方法,但并不认为这是一个有效的好方法
public static void main(String [] args){
try
{
System.setProperty("webdriver.chrome.driver", "config/chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("https://www.example.com");
driver.manage().window().maximize();
String cookieDomain = driver.manage().getCookieNamed("sessID").getDomain();
String cookieValue = driver.manage().getCookieNamed("sessID").getValue(); // Example : ASGTFF-node-1
String cookiePath = driver.manage().getCookieNamed("sessID").getPath();
Date expiryDate = driver.manage().getCookieNamed("sessID").getExpiry();
boolean isSecure = driver.manage().getCookieNamed("sessID").isSecure();
char currentNodeValue = cookieValue.charAt(cookieValue.length() - 1); // suppose this comes as 1 but could be different everytime
String newCookieValue = cookieValue.substring(0, cookieValue.length() - 1);
System.out.println("New Cookie Value : " + isSecure);
String newCookieValueAfterAdd = newCookieValue.concat("2");
System.out.println("New Cookie Value After Add : " + newCookieValueAfterAdd); // This will become ASGTFF-node-2
driver.close();
invokeNewSession(newCookieValueAfterAdd, cookiePath, expiryDate, isSecure, cookieDomain);
}
catch(Exception e)
{
e.printStackTrace();
}
}
private static void invokeNewSession(String newCookieValueAfterAdd, String cookiePath, Date expiryDate, boolean isSecure, String cookieDomain) {
try
{
Cookie ck = new Cookie("sessID",newCookieValueAfterAdd, cookieDomain, cookiePath, expiryDate, isSecure);
//System.setProperty("webdriver.chrome.driver", "C:\\Shashank\\Java\\Selenium\\chromedriver_win32\\chromedriver.exe");
System.setProperty("webdriver.chrome.driver", "config/chromedriver.exe");
WebDriver driverNew = new ChromeDriver();
driverNew.get("https://www.example.com");
driverNew.manage().window().maximize();
driverNew.manage().addCookie(ck);
String cookieValueNew = driverNew.manage().getCookieNamed("sessID").getValue();
String cookiePathNew = driverNew.manage().getCookieNamed("sessID").getPath();
Date expiryDateNew = driverNew.manage().getCookieNamed("sessID").getExpiry();
boolean isSecureNew = driverNew.manage().getCookieNamed("sessID").isSecure();
System.out.println("Cookie value for JSESSIONID : " + cookieValueNew);
System.out.println("Cookie Path for JSESSIONID : " + cookiePathNew);
System.out.println("Cookie Expiry for JSESSIONID : " + expiryDateNew);
System.out.println("Cookie Secure for JSESSIONID : " + isSecureNew);
}
catch(Exception e)
{
e.printStackTrace();
}
}