클린 코드를 위한 테스트 주도 개발 4

2016-09-22

원인을 알 수 없음?

selenium.common.exceptions.StaleElementReferenceException: Message: The element reference is stale. Either the element is no longer attached to the DOM or the page has been refreshed.

.implicitly_wait(3)도 넣어놨는데 이 에러는 왜 생기는지 잘 모르겠다. 종종 생기다가 그냥 몇 초 뒤에 다시 테스트 돌리면 말끔하게 사라진다.

p.107-1부 끝까지

    기능테스트를 하면 같은 코드인데도 이 3가지 에러가 반복해서 나온다. 단위테스트는 책에 나온대로 잘 되었다. 브라우저 처리속도 문제인가.. 라고 추측은 하는데 잘 모르겠다.

self.assertNotEqual(francis_list_url, edith_list_url)
AssertionError: 'http://localhost:8081/lists/the-only-list-in-the-world/' == 'http://localhost:8081/lists/the-only-list-in-the-world/'
self.assertRegex(edith_list_url, '/lists/.+')
AssertionError: Regex didn't match: '/lists/.+' not found in 'http://localhost:8081/'
self.assertNotEqual(francis_list_url, edith_list_url)
AssertionError: 'http://localhost:8082/lists/the-only-list-in-the-world/' == 'http://localhost:8082/lists/the-only-list-in-the-world/'

    해결한 방법은 모든 inputbox.send_keys(Keys.ENTER) 뒤에 time.sleep(1)을 넣었다. p.20 의 상단에 아래와 같은 주의사항이 적혀져 있다.

셀레늄은 비교적 안정적으로 페이지 로딩이 끝날 때까지 기다렸다가 테스트를 실행하지만 완벽하진 않다. implicitly_wait는 필요에 따라 지정한 시간(초 단위)만큼 동작을 대기 상태로 둘 수 있다. ..(중략).. implicitly_wait를 너무 신뢰해서는 안 된다. 항상 제대로 동작하는 것이 아니기 때문이다. 애플리케이션 구조가 간단한 경우에는 동작하지만, 복잡한 애플리케이션에 대해서는 '명시적인' 대기 알고리즘을 별도로 작성해야 한다.

    값넣고 출력하는게 전부인지라 고작 1부까지 만든게 복잡한 어플리케이션인가?? 싶었다. 그래서 time.sleep대신에 implicitly_wait으로 전부 바꿨는데 다시 기능테스트 에러가 나왔다.