Selenium - Bii o ṣe le Mu awọn Kuki pada sipo ni Ferese Burausa Tuntun

Ṣebi a ni lati ni idanwo fun iṣẹlẹ wọnyi:

1. Lọ si oju-iwe iwọle ki o wọle si ohun elo naa
2. Pa ẹrọ lilọ kiri ayelujara naa
3. Ṣii ẹrọ lilọ kiri lori ayelujara ki o lọ si oju-iwe iwọle - olumulo ko yẹ ki o wo fọọmu iwọle ki o yẹ ki o ti wọle tẹlẹ.

Lori iwọle akọkọ, awọn kuki ti wa ni fipamọ ni ẹrọ aṣawakiri naa. Ninu WebDriver, nigbati window ẹrọ aṣawakiri ba ti wa ni pipade, gbogbo data igba ati awọn kuki paarẹ, nitorinaa idanwo ti iwoye ti o wa loke ko ṣeeṣe.


Ni Oriire, WebDriver ni iṣẹ-ṣiṣe lati ka awọn kuki lati ẹrọ aṣawakiri ṣaaju ki o to pa ati lẹhinna mu awọn kuki pada si oju-iwe ẹrọ aṣawakiri tuntun.

import org.openqa.selenium.By; import org.openqa.selenium.Cookie; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; import org.testng.Assert; import java.util.Set; public class CookieTest {
WebDriver driver;
@Test
public void login_state_should_be_restored() {
driver = new FirefoxDriver();

driver.get('http://www.example.com/login');
driver.findElement(By.id('username')).sendKeys('admin');
driver.findElement(By.id('password')).sendKeys('12345');
driver.findElement(By.id('login')).click();

Assert.assertTrue(


driver.findElement(By.id('welcome')).isDisplayed());

//Before closing the browser, read the cookies
Set allCookies = driver.manage().getCookies();

driver.close();

//open a new browser window
driver = new FirefoxDriver();

//restore all cookies from previous session
for(Cookie cookie : allCookies) {

driver.manage().addCookie(cookie);
}

driver.get('http://www.example.com/login'); //Login page should not be disaplyed
Assert.assertTrue(


driver.findElement(By.id('welcome')).isDisplayed());

driver.close();
} }