Skip to content

Commit

Permalink
refactor: #12 변경된 웹페이지(23.7.17)를 기준으로 영업시간과 휴무일을 정상적으로 추출할 수 있도록 리팩토링
Browse files Browse the repository at this point in the history
  • Loading branch information
Wo-ogie committed Jul 17, 2023
1 parent 0e24786 commit 3524071
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,12 @@ public BusinessHoursDto getBusinessHours(WebElement mArticle) throws NoSuchEleme
return createBusinessHours(openingHours, null);
}

// TODO: div.inner_floor, div.displayPeriodList ul이 반드시 존재한다고 가정한 코드. 아닌 경우가 있다면 try-catch로 감싸야한다.
WebElement operationList = mArticle.findElement(By.cssSelector("div.details_placeinfo div.fold_floor > div.inner_floor"));
String openingHours = operationList.findElement(By.cssSelector("ul:nth-child(2)")).getText();
return createBusinessHours(openingHours, getClosingHours(operationList));
String openingHours = operationList.findElement(By.cssSelector("div.displayPeriodList > ul:nth-child(2)")).getText();
System.out.println(openingHours);
String closingHours = getClosingHours(operationList);
return createBusinessHours(openingHours, closingHours);
}

private BusinessHoursDto createBusinessHours(String openingHours, String closingHours) {
Expand Down Expand Up @@ -72,12 +75,7 @@ private String makeClosingHours(List<Day> missingDays) {

private String getClosingHours(WebElement operationList) {
try {
if (operationList.findElement(By.cssSelector("strong:nth-child(3)")).getText().equals("휴무일")) {
return operationList.findElement(By.cssSelector("ul:nth-child(4)")).getText();
} else if (operationList.findElement(By.cssSelector("strong:nth-child(5)")).getText().equals("휴무일")) {
return operationList.findElement(By.cssSelector("ul:nth-child(6)")).getText();
}
return null;
return operationList.findElement(By.cssSelector("div.displayOffdayList > ul:nth-child(2)")).getText();
} catch (NoSuchElementException e) {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.zelusik.scraping.dto.place.OpeningHourDto;
import com.zelusik.scraping.dto.place.PlaceInfoResponse;
import com.zelusik.scraping.dto.place.TimeDto;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
Expand All @@ -20,8 +21,9 @@
import static org.junit.jupiter.params.provider.Arguments.arguments;

@DisplayName("[Integration] Selenium을 활용해 장소 정보 가져오기")
@Disabled("실제 가게 정보는 수시로 바뀔 수 있으므로 기능의 정상적인 동작을 확인하기에 불안정하다. 따라서 평소에는 disabled 해놓고 필요할 때 테스트하는 방식으로 사용한다.")
@SpringBootTest
class PlaceScrapingTest {
class KakaoPlaceScrapingTest {

@Autowired
private KakaoPlaceScrapingController sut;
Expand All @@ -37,6 +39,7 @@ void getKakaoPlaceInfoTest(String kakaoPid, List<OpeningHourDto> openingHours, S

// then
if (openingHours != null) {
assertThat(actualResult.getOpeningHours()).isNotNull();
assertThat(actualResult.getOpeningHours().size()).isEqualTo(openingHours.size());
for (int i = 0; i < openingHours.size(); i++) {
assertThat(actualResult.getOpeningHours().get(i).getDay()).isEqualTo(openingHours.get(i).getDay());
Expand All @@ -60,7 +63,7 @@ static Stream<Arguments> kakaoPlaceInfos() {
OpeningHourDto.of(FRI, TimeDto.of(LocalTime.of(10, 30), LocalTime.of(15, 30))),
OpeningHourDto.of(SAT, TimeDto.of(LocalTime.of(10, 30), LocalTime.of(15, 30))),
OpeningHourDto.of(SUN, TimeDto.of(LocalTime.of(10, 30), LocalTime.of(15, 30)))),
"월요일", null),
"월요일", "https://app.catchtable.co.kr/ct/shop/moru"),
arguments("308342289",
List.of(OpeningHourDto.of(MON, TimeDto.of(LocalTime.of(11, 30), LocalTime.of(22, 0))),
OpeningHourDto.of(TUE, TimeDto.of(LocalTime.of(11, 30), LocalTime.of(22, 0))),
Expand All @@ -71,13 +74,13 @@ static Stream<Arguments> kakaoPlaceInfos() {
OpeningHourDto.of(SUN, TimeDto.of(LocalTime.of(11, 30), LocalTime.of(22, 0)))),
null, "https://www.instagram.com/toma_wv/"),
arguments("25001083",
List.of(OpeningHourDto.of(MON, TimeDto.of(LocalTime.of(7, 30), LocalTime.of(23, 59))),
OpeningHourDto.of(TUE, TimeDto.of(LocalTime.of(7, 30), LocalTime.of(23, 59))),
OpeningHourDto.of(WED, TimeDto.of(LocalTime.of(7, 30), LocalTime.of(23, 59))),
OpeningHourDto.of(THU, TimeDto.of(LocalTime.of(7, 30), LocalTime.of(23, 59))),
OpeningHourDto.of(FRI, TimeDto.of(LocalTime.of(7, 30), LocalTime.of(23, 59))),
OpeningHourDto.of(SAT, TimeDto.of(LocalTime.of(7, 30), LocalTime.of(23, 59))),
OpeningHourDto.of(SUN, TimeDto.of(LocalTime.of(7, 30), LocalTime.of(23, 59)))),
List.of(OpeningHourDto.of(MON, TimeDto.of(LocalTime.of(8, 0), LocalTime.of(0, 0))),
OpeningHourDto.of(TUE, TimeDto.of(LocalTime.of(8, 0), LocalTime.of(0, 0))),
OpeningHourDto.of(WED, TimeDto.of(LocalTime.of(8, 0), LocalTime.of(0, 0))),
OpeningHourDto.of(THU, TimeDto.of(LocalTime.of(8, 0), LocalTime.of(0, 0))),
OpeningHourDto.of(FRI, TimeDto.of(LocalTime.of(8, 0), LocalTime.of(0, 0))),
OpeningHourDto.of(SAT, TimeDto.of(LocalTime.of(8, 0), LocalTime.of(0, 0))),
OpeningHourDto.of(SUN, TimeDto.of(LocalTime.of(8, 0), LocalTime.of(0, 0)))),
null, "http://www.mcdonalds.co.kr/"),
arguments("24529744",
List.of(OpeningHourDto.of(MON, TimeDto.of(LocalTime.of(11, 30), LocalTime.of(22, 0))),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.zelusik.scraping.dto.place.TimeDto;
import com.zelusik.scraping.service.KakaoPlaceScrapingService;
import com.zelusik.scraping.util.OpeningHoursConverter;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
Expand Down Expand Up @@ -77,19 +78,15 @@ void existsButtonAndOpeningAndClosingHours_whenGetBusinessHours_thenReturnResult
String closingHours = "월요일\n화요일";
BusinessHoursDto expectedResult = new BusinessHoursDto(openingHourDtos, closingHours);

given(mArticle.findElement(By.cssSelector("div.cont_essential > div.details_placeinfo " +
"div.location_detail.openhour_wrap > div.location_present a.btn_more"))).willReturn(button);
given(mArticle.findElement(By.cssSelector("div.cont_essential > div.details_placeinfo div.location_detail.openhour_wrap > div.location_present a.btn_more"))).willReturn(button);
willDoNothing().given(button).click();
given(mArticle.findElement(By.cssSelector("div.details_placeinfo div.fold_floor > div.inner_floor"))).willReturn(operationList);
WebElement openingHoursElem = createWebElemMock("openingHours");
given(operationList.findElement(By.cssSelector("ul:nth-child(2)"))).willReturn(openingHoursElem);
given(operationList.findElement(By.cssSelector("div.displayPeriodList > ul:nth-child(2)"))).willReturn(openingHoursElem);
given(openingHoursElem.getText()).willReturn(openingHours);
WebElement closingHoursTitleElem = createWebElemMock("closingHoursTitle");
given(operationList.findElement(By.cssSelector("strong:nth-child(3)"))).willReturn(closingHoursTitleElem);
given(closingHoursTitleElem.getText()).willReturn("휴무일");
WebElement closingHoursElem = createWebElemMock("closingHours");
given(operationList.findElement(By.cssSelector("ul:nth-child(4)"))).willReturn(closingHoursElem);
given(closingHoursElem.getText()).willReturn(closingHours);
given(operationList.findElement(By.cssSelector("div.displayOffdayList > ul:nth-child(2)"))).willReturn(closingHoursTitleElem);
given(closingHoursTitleElem.getText()).willReturn(closingHours);
given(converter.parseStrToOHs(openingHours)).willReturn(openingHourDtos);

// when
Expand All @@ -99,6 +96,11 @@ void existsButtonAndOpeningAndClosingHours_whenGetBusinessHours_thenReturnResult
verifyGetBusinessHours(openingHours, expectedResult, actualResult);
}

@Disabled("""
23.7.17 기준 "공휴일" 등의 정보가 div.displayPeriodList에 포함되었다.
그러므로 현재 코드에서는 "공휴일" 등의 정보를 전혀 추출하지 않고, 그렇기 때문에 바로 위의 test(existsButtonAndOpeningAndClosingHours_whenGetBusinessHours_thenReturnResult)와 로직이 동일하다.
어차피 "공휴일" 등의 정보는 현재 필요하지 않지만, 추후 필요할 수도 있으니 단지 test case를 남기기 위해 우선 이 test는 disabled 처리한다.
""")
@DisplayName("영업시간 조회 - 버튼을 눌러 정보 열람 후, 영업시간과 휴무일 모두 있으나 휴무일 이전에 다른 정보(공휴일 등)가 함께 있는 경우")
@Test
void existsButtonAndOpeningAndClosingHoursAndAdditionalInfo_whenGetBusinessHours_thenReturnResult() {
Expand Down Expand Up @@ -153,9 +155,9 @@ void existsButtonAndOpeningHours_whenGetBusinessHours_thenReturnResult() {
willDoNothing().given(button).click();
given(mArticle.findElement(By.cssSelector("div.details_placeinfo div.fold_floor > div.inner_floor"))).willReturn(operationList);
WebElement openingHoursElem = createWebElemMock("openingHours");
given(operationList.findElement(By.cssSelector("ul:nth-child(2)"))).willReturn(openingHoursElem);
given(operationList.findElement(By.cssSelector("div.displayPeriodList > ul:nth-child(2)"))).willReturn(openingHoursElem);
given(openingHoursElem.getText()).willReturn(openingHours);
given(operationList.findElement(By.cssSelector("strong:nth-child(3)"))).willThrow(NoSuchElementException.class);
given(operationList.findElement(By.cssSelector("div.displayOffdayList > ul:nth-child(2)"))).willThrow(NoSuchElementException.class);
given(converter.parseStrToOHs(openingHours)).willReturn(openingHourDtos);

// when
Expand Down

0 comments on commit 3524071

Please sign in to comment.