Home > Net >  How to use the django test's database with Selenium?
How to use the django test's database with Selenium?

Time:01-31

I am testing my django app with django TestCase class. For unit tests and integrations tests I encountered no problem with the database django create then destroy for the tests. But now i want to do some functional test using selenium. The problem is that selenium seem to not be able to access the db. Here is the test code of my test :

class HostTest(LiveServerTestCase, TestCase):


    def setUp(self):

        self.driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()))

        # Setting up temporary test database

        # Setting up products
        self.product1 = Product.objects.create(
            id=1,
            name="apple",
            url="apple_url",
            nutriscore="A",
            img_url="apple_img_url",
            kcal=101,
            fat=201,
            protein=301,
            sugar=401,
        )


    def tearDown(self):
        self.driver.close()

    def test_new_user_reserach_and_add_favorite(self):
        driver = self.driver
        driver.get(self.live_server_url)
        self.assertIn("Accueil", driver.title)

        search_bar = driver.find_element_by_name("product_searched")
        search_bar.send_keys("apple")
        search_bar.send_keys(Keys.ENTER)
        self.assertIn("Recherche", driver.title)

        product = driver.find_element_by_class_name('product-presentation')

For this I have an error at the last line, and the error is long but its basically tell me that selenium cant find the element. I tried to print product1 in the test and it work. That why I'm pretty sure that the problem is with Selenium.

Here is the code of my view :

def product_research(request):
    no_repetition_result = []
    vectors = SearchVector('name', weight='A')   SearchVector('category__name', weight='B')
    query = SearchQuery(f'{request.GET.get("product_searched")}')

    research_result = Product.objects.annotate(rank=SearchRank(vectors, query)).order_by('-rank')

    for product in research_result:
        if product not in no_repetition_result:
            no_repetition_result.append(product)

    context = {
        "results": no_repetition_result,
    }
    request.session['research_parameter'] = request.GET.get("product_searched")
    return render(request, 'main_site/product_research.html', context)

And the html code charged to make appear the product on my web page :

<div >
    {% for product in results %}
        <div 
            <div >
                <a  href="{% url 'main_site:product' product.id %}">
                    <img  src="{{ product.img_url }}">
                </a>
            </div>
        </div>
    {% endfor %}
</div>

Note: When I take a look at the code of the page selenium open while testing, I dont see any product.

CodePudding user response:

Check this answer

This is the same problem that you are facing and has a clean explanation.

  •  Tags:  
  • Related