Selenium - Me pehea te whakaora i nga Pihikete i te Matapihi Tirohanga Hou

Whakaarohia me whakamatau maatau mo nga ahuatanga e whai ake nei:

1. Haere ki te whaarangi whakauru ka takiuru ki te tono
2. Katia te tirotiro
3. Whakatuwherahia te kaitirotiro ka haere ki te whaarangi takiuru - kaua te kaiwhakamahi e kite i te puka takiuru me te whakauru kua oti te whakauru.

I te takiuru tuatahi, ka penapenahia nga pihikete ki te kaitirotiro. I roto i te WebDriver, ka kati ana te matapihi tirotiro, ka mukua katoahia nga waahanga o te waahanga me nga pihikete, na reira kaore e taea te whakamatautau i nga ahuatanga o runga ake nei.

Waimarie, he mahinga ta WebDriver hei panui i nga pihikete mai i te kaitirotiro i mua i te katinga ka whakahoki i nga pihikete ki te matapihi tirotiro hou.

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();
} }