Executing two pytest tests, where one is expected to be successful and the other to fail

I've been given an assignment that requires me to create a pytest test with specific criteria:

· The first test should PASS if the string " Right-click in the box below to see one called 'the-internet' " is found on the page.

· The second test should FAIL if the string "Alibaba" is not found on the same page.

This is my code snippet for the tests:

import pytest

def test_text1():
    text1 = "Right-click in the box below to see one called 'the-internet'"
    if text1 in driver.page_source:
        assert text1 in driver.page_source

def test_text2():
    text2 = "Alibaba"
    if text2 in driver.page_source:
        assert text2 in driver.page_source

I'm currently working on writing a Python script and running the pytest tests using the Pycharm terminal. However, even though I expect the first test to pass and the second one to fail, both tests are passing. Do you have any suggestions on how to address this issue? Thank you!

Answer №1

It is unnecessary to use an if statement within an assert:

import pytest

def test_text1():
    text1 = "Right-click in the box below to see one called 'the-internet'"
    assert text1 in driver.page_source

def test_text2():
    text2 = "Alibaba"
    assert text2 in driver.page_source

An even better approach would be to utilize parametrize:

@pytest.mark.parametrize("text",
["Right-click in the box below to see one called 'the-internet'",
"Alibaba"]
)
test_contains_text(text, driver):
    assert text in driver.page_source

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Encountering difficulties extracting audio from the webpage

Attempting to extract audio(under experience the sound) from 'https://www.akrapovic.com/en/car/product/16722/Ferrari/488-GTB-488-Spider/Slip-On-Line-Titanium?brandId=20&modelId=785&yearId=5447'. The code I have written is resulting in an ...

Unable to Run Test Case using TestNG

import org.testng.annotations.Test; import org.testng.annotations.BeforeMethod; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import org.testng.annotations.AfterMethod; public clas ...

Details about the Chrome driver can be found in the console

While running my selenium tests with ChromeDriver, I am seeing this message in the console: Starting ChromeDriver 2.15.322448 (521791b310fec1797c81ea9a20326839860b7d3) on port 15823 Is there a method to hide or prevent this message from appearing in the ...

How to use Python Selenium to run multiple test instances by importing a text/csv file

Is there a way to efficiently call multiple files by importing text files or csv files and running selenium tests on each input until all are complete? Usually, the browser closes after performing just one task. For instance, if I have the following input ...

Using Selenium in Python, you can programmatically open a link in a new tab without actually navigating

Currently, I am utilizing selenium for web scraping and I am attempting to open a new tab with a link while still remaining on the initial tab. This is what I have so far: first_link.send_keys(Keys.CONTROL + Keys.RETURN) Instead of opening the link in a ...

Converting a table into a collection of dictionaries using BeautifulSoup

I am encountering some difficulties in scraping a specific table and converting it into a list of dictionaries. The table I am interested in scraping can be found on this page My objective is to scrape only the "Batting" table. Below is the code I have b ...

The combination of spring, cucumber, and Selenium's fluent wait is causing my driver

I am attempting to implement fluent wait in my code @Component @Scope(SCOPE_CUCUMBER_GLUE) public class UserCreationPageImpl extends BaseBinariosPage implements UserCreationPage { Wait<WebDriver> wait = new FluentWait<WebDriver>( driver ) ...

Difficulty in finding element in iframe using ember and Selenium

This is my first time working on automation using cucumber. The UI is in an iFrame and built with ember by another team, so any changes to the UI are not feasible. I am attempting to locate a text field and populate it (the final step). However, I continu ...

What are the steps for utilizing `pytest` in Python?

Currently, I am involved in a project that has recently transitioned to using the pytest and unittest framework. Previously, I would run my tests from Eclipse to utilize the debugger for analyzing test failures by placing breakpoints. However, this method ...

Neglecting to log out of Gmail using Selenium WebDriver

Following my successful login code for gmail through webdriver, I attempted to implement the logout functionality using the code below, but unfortunately encountered an error: // To open the account menu where the sign out button is located driver.findEle ...

Find the Xpath to locate the image source on the Flipkart website

Looking for the XPath to click on an image in the mobiles section of Flipkart's website, specifically the realme shop. I have identified the necessary XPath and prepared the code as follows: package Test; import org.openqa.selenium.By; ...

Capture images with iPad's camera using Selenium and Appium

I'm currently in the process of automating an iOS application on an iPad using Selenium and Appium. One feature of the app involves opening the default camera application on the iPad, allowing the user to take a picture. The task at hand is to automat ...

Having a tough time getting Selenium to successfully complete the automated checkout process

After experimenting with Selenium and attempting to automate the checkout process, I've encountered a stumbling block in the final part of my code. Despite numerous configurations of 'sleeps' and 'implicit_waits', I keep receiving ...

Tips for verifying and controlling dropdown values using WebDriver?

Is there a way to check the status of percentage values in a drop-down, where some are enabled and others disabled? I am looking for a method to obtain the XPath of a specific value and determine if it is enabled or disabled. Click on the image below for ...

Selecting a file using OpenFileDialog in C#

I'm facing a small issue - I'm not sure how to select a file and open it in the Mozilla OpenFileDialog. Initially, I utilize Selenium to open the dialog by clicking "Browse" and then I need to input a filename (I have the exact location via an E ...

Extracting information from a webpage

I am trying to extract the text from the XPath provided below using Selenium. However, I keep encountering the following traceback error: date = driver.find_element_by_xpath('//[@id="General"]/fieldset/dl/dd[5]/text()').text() print(date) Error ...

Finding the number of child elements using Selenium WebDriver with JavaScript

If my HTML looks like this <select class="list"> <option val="00"></option> <option val="01">One</option> </select> Although the JavaScript test file is running successfully, I am attempting to determine the number ...

Troubleshooting Selenium: Encountering connectivity issues with Chrome upon introducing the `--remote-debugging-port=<port>` parameter to Edge options

Code: EdgeOptions edgeoptions = new EdgeOptions(); edgeoptions.addArguments("--remote-debugging-port=14837"); edgeoptions.addArguments("--start-maximized"); edgeoptions.addArguments("user-data-dir=C:\\User ...

Developing a large number of email addresses using Selenium

I am looking to automate the process of creating multiple email addresses using Selenium. While I can already create one email address programmatically, I have a specific list in mind for the new accounts. How can I efficiently sign up for multiple email ...

adjusting the request process in scrapy

import scrapy from selenium import webdriver from scrapy.loader import ItemLoader from scrapy.loader.processors import Join,MapCompose from scrapy.crawler import CrawlerProcess from scrapy.http import HtmlResponse class DemoSpider(scrapy.Spider): name ...