ለሚቀጥለው ሁኔታ መሞከር አለብን እንበል
1. ወደ የመግቢያ ገጽ ይሂዱ እና ወደ ማመልከቻው ይግቡ
2. አሳሹን ይዝጉ
3. አሳሹን ይክፈቱ እና ወደ የመግቢያ ገጹ ይሂዱ - ተጠቃሚው የመግቢያ ቅጹን ማየት የለበትም እና ቀድሞውኑ መግባት አለበት።
በመጀመሪያው መግቢያ ላይ ኩኪዎች በአሳሹ ውስጥ ይቀመጣሉ። በዌብ ድራይቨር ውስጥ የአሳሽ መስኮቱ ሲዘጋ ሁሉም የክፍለ-ጊዜው መረጃዎች እና ኩኪዎች ይሰረዛሉ ፣ ስለሆነም ከላይ የተጠቀሰው ሁኔታ መሞከር የማይቻል ይሆናል።
እንደ እድል ሆኖ ፣ ድርድራይቨር ከመዘጋቱ በፊት ኩኪዎቹን ከአሳሹ ለማንበብ እና በአዲሱ የአሳሽ መስኮት ውስጥ ያሉትን ኩኪዎች እንዲመልሱ የሚያስችል ተግባር አለው ፡፡
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();
} }