demand
Since the simulation request for login is too complicated, we want to use Selenium to open Google Chrome to get the cookie value after login, and then carry the cookie content to request the relevant interface of the website to automate the test.
Selenium
Selenium is a tool for web application testing. Selenium tests run directly in the browser, as if a real user were doing it. Supported browsers include IE (7, 8, 9, 10, 11), Mozilla Firefox, Safari, Google Chrome, Opera, etc. The main features of this tool include: Browser compatibility testing - test your application to see if it works well on different browsers and operating systems. Test system functionality – Create regression tests to verify software functionality and user requirements. It supports automatic recording of actions and automatic generation of test scripts in different languages such as .Net, Java, Perl, etc.
First, let's create a new C# console project with the project name "chrome-cookie-demo" and the .NET framework version is 4.7.2.
The nuget command is as follows:
After the installation of Selenium.WebDriver.ChromeDriver, a chromedriver.exe file will be generated in the project debug directory. Selenium.WebDriver is understood as an abstract interface, and then each browser implements this interface by itself, Selenium.WebDriver will call Selenium.WebDriver.ChromeDriver(chromedriver.exe), chromedriver.exe will start the natively installed Google Chrome according to the obtained parameters, and then perform the corresponding operation.
Of course, there is a prerequisite,The computer must have Google Chrome installed。
The code is as follows:
The error is as follows:
System.InvalidOperationException: session not created: This version of ChromeDriver only supports Chrome version 83
This means that the current driver does not support version 83 of Google Chrome, check the version installed on the machine as follows:
(Version 71.0.3578.98)
Upgrading your native Google Chrome and restarting it will fix it (or install the browser version that meets the requirements).
We try to re-run the chrome-cookie-demo project and find that the site can be opened normally, but,The web page is not logged in。 When you click on the browser to open this site, the status is logged in.
The solution is to set the user-data-dir parameter with the following code:
We restart the program and get the following error:
Unhandled exception: OpenQA.Selenium.WebDriverException: invalid argument: user data directory is already in use, please specify a unique value for --user-data-dir argument, or don't use -- user-data-dir at OpenQA.Selenium.Remote.RemoteWebDriver.UnpackAndThrowOnError(Response errorResponse) at OpenQA.Selenium.Remote.RemoteWebDriver.Execute(String driverCommandToExecute, Dictionary'2 parameters) In OpenQA.Selenium.Remote.RemoteWebDriver.StartSession(ICapabilities desiredCapabilities) In OpenQA.Selenium.Remote.RemoteWebDriver: ctor(ICommandExecutor commandExecutor, ICapabilities desiredCapabilities) In OpenQA.Selenium.Chrome.ChromeDriver: ctor(ChromeDriverService service, ChromeOptions options, TimeSpan commandTimeout) In OpenQA.Selenium.Chrome.ChromeDriver: ctor(ChromeDriverService service, ChromeOptions options) In chrome_cookie_demo. Program.Main(String[] args) Location C:\Users\itsvse_pc\source\repos\chrome-cookie-demo\chrome-cookie-demo\Program.cs: line number 36 Roughly speaking, the folder has been occupied, please replace it, the solution:Close your open Google Chrome and use the user data folder for the "chrome-cookie-demo" program we wrote。
Get web cookies
driver.manage().getCookies(); // Return The List of all Cookies driver.manage().getCookieNamed(arg0); //Return specific cookie according to name driver.manage().addCookie(arg0); //Create and add the cookie driver.manage().deleteCookie(arg0); // Delete specific cookie driver.manage().deleteCookieNamed(arg0); // Delete specific cookie according Name driver.manage().deleteAllCookies(); // Delete all cookies Documentation:The hyperlink login is visible.
We test to get all web cookies
Discovery,You can read the cookie information of "HttpOnly", as shown in the figure below:
Of course, we need to convert the cookie information into a string, and then simulate the request submission interface, and attach the method of converting it to a string in the source code.
Source code download:
The hyperlink login is visible.
(End)
|