Przejdź do głównej zawartości

WebDriver - 2 triki, które zaoszczędzą kilka godzin Twojego czasu.

Osoby, które pracują ze mną na co dzień doskonale wiedzą, że jestem wielkim fanem testów automatycznych.
Ostatnio w pracy napotkałem kilka ciekawych przypadków, którymi chciałbym się dziś z Wami podzielić.

Na początek przypomnijmy poziomy testowania:
1. Testy jednostkowe
2. Testy integracyjne
3. Testy Systemowe

O ile testy jednostkowe są prawie zawsze stosowane przez programistów, o tyle dwa pozostałe poziomy są czasami zaniedbywane.

Dzisiaj skupiamy się na testach systemowych. Można je robić ręcznie klikając, wpisując i czytając co pokazuje nam przeglądarka www. Można też zautomatyzować cały ten proces.
I do tego właśnie służy WebDriver.
Pokażę w tym wpisie dwa triki, które pomogą Ci zaoszczędzić sporo czasu jeśli programujesz testy akceptacyjne w środowisku .NET(C#) z wykorzystaniem biblioteki Selenium WebDriver.

Mam coś dla osób chcących zwiększyć swoje umiejętności w temacie programowania testów systemowych w WebDriverze: http://toolsqa.com/selenium-c-sharp/.
Szczególnie mogę polecić mój artykuł o przełączaniu kontekstu między oknami na stronie. Znajduje się on w tej właśnie serii.

Teraz Przejdźmy do rzeczy.

Problem numer 1: Próbujesz zastosować konstrukcję Implicit Wait, która nie przynosi skutku bo elementu nie udaje się znaleźć albo wykonać na nim jakiejś akcji.
Tak wygląda implicit wait:
WebBrowser.Driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(15);

Wtedy stosujesz metodę explicit wait, w której możesz zdefiniować na co czekasz - określasz, że czekasz aż element będzie na przykład możliwy do kliknięcia.
Uruchamiasz test - nadal nie działa.
Trik numer 1:
Zastosuj wtedy najprostsze rozwiązanie, takie jak pasywne oczekiwanie jedną sekundę:
Thread.Sleep(1000); - jest to podejście nieefektywne, ale czasami jedyne.

Problem numer 2: Próbujesz zaprogramować zdarzenie kliknięcia danego elementu, ale z jakiegoś powodu przycisk pozostaje niekliknięty. To z kolei powoduje, że Twój test kończy się błędem.

Oto kod, który może nie zadziałać, wbrew swojej prostej i oczywistej konstrukcji:
var saveButton = WebBrowser.Driver.FindElement(By.Id("btn-save"));
saveButton.Click();

Trik numer 2:
Aby rozwiązać problem zastąpiłem wyżej pokazany kod następującym rozwiązaniem:

var saveButton = WebBrowser.Driver.FindElement(By.Id("btn-save"));
saveButton.SendKeys(Keys.Return);

I zadziałało.

To tyle trików na dzisiaj. Dziękuję za uwagę. Więcej już niebawem.

Komentarze

Popularne posty z tego bloga

Unleash the power of ChatGPT in your C# applications with NGpt - C# OpenAI GPT client

Meet NGpt - OpenAI GPT C# client library that will make your conversations with ChatGPT a breeze! Have you ever been struggling building your own smart AI application in C#? Well, now you can use ChatGpt inside your application! Introducing NGpt, the powerful .NET library that lets you integrate ChatGPT seamlessly into your C# applications. No more nigts spent on building your own AI solitions, no more convoluted logic and building complex business rules - just pure AI bliss. In this blog post, we'll dive into the wonderful world of NGpt and show you how easy it is to use. You might even find a few laughs along the way. The Magic of NGpt NGpt is a transient fault-tolerant .NET 6 OpenAI client that simplifies ChatGPT integration for C# developers. It's like your friendly neighborhood Spider-Man - always there to help you out when you need it most. With just your OpenAI API key, you can start coding AI applications in C# faster than you can say "Peter Parker." Let...

Learning e24cloud.com API

Today I'll show you how to use API in order to create virtual server in the cloud directly from your code. We're going write a script in PHP language and use Amazon SDK API to connect with e24cloud.com, which is the biggest and most modern hosting provider in Poland. First we need to install Amazon SDK http://docs.aws.amazon.com/aws-sdk-php/v3/guide/getting-started/installation.html One important note: We can use the latest version of amazon SDK but we also need to do one trick. We'll use signatureV2 instead ov Signaturev4 as e24cloud doesn't support SitnatureV4 yet. You can download it here: https://github.com/aws/aws-sdk-php-v3-bridge We need to download SDK and the bridge which allows us to use Signaturev2 to the same folder, so that require instruction work seamlessly in the form that I provided in the example below. Then go to your admin panel in e24cloud.com to find two values: a key and a secret. In the following code you need to paste those 2 values. ...

Connect to Azure from command line on Windows

When working with Azure we could go two ways: 1) using Azure portal and work in UI from your web browser 2) Connect to Azure from command line and run commands by typing in text in CMD. I already wrote about option 1 but today I'll show you how to start with option 2 which is Command line. First we need to download MSI installer from Microsoft website After sucessfull instalation you're ready to test az command. Open CMD and type in 'az' Now let's log in with your Azure credentials. Just type in "az login" in cmd. You should be redirected from command line to web browser and prompted for username and account in web browser. When you confirm your identity you'll be presented the screen like this in command line: