0

My group usually doesn't handle web testing, but for this project some of it falls on me and I'm working out automation as I go. The site isn't public, or I'd just provide it for reference.

Using Selenium 2 and Python, I've managed to make some requested tests by reading documentation (and stack exchange) but I've hit a wall on checking text strings across languages.

There's an always-present language selector element on the site that provides a menu of supported languages and changes the display language for text throughout the site. All of the CSS selectors used stay the same throughout these language changes.

I know better than to make a separate copy of the test case for each language, since that will make updating and bugfixing too complicated. I'm not really sure what to do instead, though.

My best guess so far is to base the test around the language selector's current setting; set it at the start of the test and then use a string of if/elif statements to handle the string in each possible language. That at least works, but it feels clunky and overcomplicated.

What better ways are there to structure a test case for a situation like this?

import os
dir = os.path.dirname(__file__)
chrome_driver_path = [[the file path]]
import unittest
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
lang = [[the CSS selector for the language change element]]
site_lng = eng #can be eng/fra/ita/ger/spa

class chrome_english(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        cls.driver = webdriver.Chrome(chrome_driver_path)
        cls.driver.maximize_window()
        cls.driver.get("https://www.url.com/"+site_lng+"/main")
        # wait for main logo or tests run before page elements have loaded
        WebDriverWait(cls.driver, 20).until(EC.presence_of_element_located((By.ID, "logo-large")))

    @classmethod
    def tearDownClass(cls):
        cls.driver.close()

    def test_navLeft_03(self):
        e = self.driver.find_element_by_css_selector('div.menu-main>div:nth-child(2)>a').text
        if self.driver.find_element_by_css_selector(lang).text=="ENGLISH":
            self.assertEqual(e, 'News')
        elif self.driver.find_element_by_css_selector(lang).text=="FRANCAIS":
            self.assertEqual(e, 'Actualités')
        elif self.driver.find_element_by_css_selector(lang).text=="ITALIANO":
            self.assertEqual(e, 'Notizie')
        elif self.driver.find_element_by_css_selector(lang).text=="DEUTCH":
            self.assertEqual(e, 'Neuigkeiten')
        elif self.driver.find_element_by_css_selector(lang).text=="ESPANOL":
            self.assertEqual(e, 'Noticias')

2 Answers 2

1

One more way:

  • Create a .properties file
  • Declare your all the language strings (could be separate .prop for different locale)
  • Whenever you want to assert for string use it from there. For example, refer below snippet:

    Assert.assertEquals((your_web_element.getText()),languageProps.getProperty("your_string_from_prop"));
    

    or:

    Assert.assertEquals((your_web_element.getText()), "your_string_for_locale");
    
0

Looks like a good case for a data-driven testing. Create a file (spreadsheet) with column for each language and row for every word you want to assert.

Then read the whole file and check whatever (one) language you want. Python is excellent for this kind of tasks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.