This article is a mirror article of machine translation, please click here to jump to the original article.

View: 177007|Reply: 27

[Source] .Net/C# Selenium automated tests get cookie values

[Copy link]
Posted on 7/5/2020 11:44:34 AM | | | |
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)

Score

Number of participants1MB+1 contribute+1 Collapse reason
jgskingstone + 1 + 1 Very powerful!

See all ratings





Previous:EF(CodeFirst) + MySQL
Next:MSSQL conditional query WHERE ignores the condition
Posted on 5/14/2022 10:22:54 AM |
Prompt:Authors are banned or removed content is automatically blocked
Posted on 5/14/2022 10:21:26 AM |
Prompt:Authors are banned or removed content is automatically blocked
Posted on 11/27/2021 5:31:04 PM |

.NET Selenium simulation is still awesome, thank you.
Posted on 7/5/2020 12:46:59 PM |
It seems good
Posted on 8/18/2020 10:45:14 AM |
Learning. Studying hard.
Posted on 10/10/2020 5:34:08 PM |
Come and learn, thank you
Posted on 10/28/2020 5:26:20 PM |
very good.  
Posted on 11/3/2020 12:20:29 AM |
Thank you~~ Learn to learn ~~~~~~~~
Posted on 11/26/2020 9:26:44 PM |
Thanks LZ for sharing
Posted on 1/24/2021 3:02:19 AM |
Why do you have to reply to see it?
Posted on 3/6/2021 8:49:30 PM |
1111111111111111
Posted on 5/2/2021 8:12:35 PM |
Learning. Studying hard.
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com